如何设置跨度textview之间的边距? [英] how set margin between spannable textview?

查看:519
本文介绍了如何设置跨度textview之间的边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建可扩展的textview并将其显示在EditText中.因此,用户可以在EditText中键入一些内容,如果用户按下了键盘的Enter键,那么在该用户可以再次输入并按下键盘的Enter键之后,我将该文本转换为可扩展的textview,那么第二个可扩展的textview将再次创建ans并将其显示在edittext但是.

I am trying to create spannable textview and showing it in EditText. So user can type something in EditText and if user pressed enter button of keyboard then i am converting this text in to spannable textview after this user can start typing again and press enter button of keyboard then again second spannable textview will create ans will show it in edittext but.

我在哪里停留?

当我创建两个可跨度的textview时,这两个textview会彼此稍微重叠.我想在这两个textview之间设置边距.

when i create two spannable textview then this two textview slightly overlapping on each other. And i want to set margin between this two textview.

我还尝试使用 LayoutParam 但没有成功.

I also tried to set margin between textview using LayoutParam but not success.

这是在EditText中显示textview彼此重叠的图像.

Here is image which showing textview overlapping on each other in EditText.

y 隐藏在美味

这是我的代码.

txtDishTags.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH
                    || actionId == EditorInfo.IME_ACTION_NEXT
                    || actionId == EditorInfo.IME_ACTION_DONE
                    || actionId == EditorInfo.IME_ACTION_GO) {
                txtDishTags.dismissDropDown();
                  if(txtDishTags.getText().toString().trim().length()>=1){
                isEntered = true;

                String[] separated = tags.split(",");
                tags = separated[separated.length-1];
                if(tags.trim().length()>=1){
                TextView tv = createContactTextView(tags);
                BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
                bd.setBounds(-20, 0, bd.getIntrinsicWidth(),
                        bd.getIntrinsicHeight());
                sb.append(tags + ",");
                sb1 = new SpannableStringBuilder();
                sb1.append(tags + ",");
                sb.setSpan(new ImageSpan(bd),
                        sb.length() - tags.length(), sb.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                sb.setSpan(clickSpan, sb.length() - tags.length(),
                        sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                txtDishTags.setText("");
                txtDishTags.setText(sb);    
                int length = sb.length();
                txtDishTags.setSelection(length, length);
                }
                }
            }
            return false;
        }
    });  

public TextView createContactTextView(String text) {
    //llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 44);
        //llp.setMargins(5, 0, 20, 0);
    TextView tv = new TextView(this);
    tv.setText(text);
    tv.setTextSize(30);     
    Typeface faceBook = Typeface.createFromAsset(getAssets(),
            "fonts/eau_sans_book.otf");
    tv.setTypeface(faceBook);   
    tv.setTextColor(getResources().getColor(R.color.backgroundcolor));
    tv.setBackgroundResource(R.color.textviewbubble);
    //tv.setLayoutParams(llp);
    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            44, r.getDisplayMetrics());
    tv.setHeight(px);
    return tv;
}

public static Object convertViewToDrawable(View view) {
    int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(viewBmp);
}  

我尝试使用以下代码在textview之间设置边距

I tried to set margin between textview using following code

 llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 44);
 llp.setMargins(5, 0, 20, 0);
 tv.setLayoutParams(llp);   

我也为Textview设置了LeftPadding,但似乎第一个textview没得到它.像

I also set LeftPadding for Textview but seems first textview not getting it.Even i set height to textview but seems textview not getting layout parameter at all. Like

int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
        44, r.getDisplayMetrics());
tv.setHeight(px);

请提供参考或提示.
预先感谢

Please give reference or hint.
Thanks in advance

推荐答案

我发现了一些问题.您需要进行指定的更改,一切都应该正常工作.

There are a few problems that I have identified. You need to make the specified changes and everything should work.

步骤1)更新setBounds参数

Step 1) Update setBounds parameters

在以下行中,将setBounds参数从-20更新为0,如下所示:

In the following line, update the setBounds parameters from -20 to 0 as follows:

BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());

这很重要,因为设置的边界错误,导致标签重叠.

This is important because you are setting the wrong bounds, which causes tags to overlap.

第2步)修复了sb.setSpan

Step 2) Fix bug in sb.setSpan

如果遵循步骤1,然后运行代码,您将意识到,尝试用ImageSpan替换文本时,您传递的是错误的值(您未考虑","(逗号)字符结尾).更新以下行以包含-1:

If you followed step 1, and you run the code, you will realize that when you attempt to replace text with ImageSpan, you are passing the wrong values (you are not taking into account the ","(comma) character in the end). Update the following line to include -1:

sb.setSpan(new ImageSpan(bd), sb.length() - tags.length() - 1, sb.length() - 1,
           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

现在您的输出将显示正确,中间带有逗号.

Now you output will appear correct and with comma in the middle.

第3步)在标签之间添加间距

要回答您最初的问题,即如何增加间距,我建议您修改代码以在不同跨度之间包含", ".您也可以修改它以仅使用" "空间.定义一个contentBetweenTags变量并将其设置为所需的值.这是您可以执行的操作:

To answer your original question, how to add spacing, I would recommend that you modify your code to include ", " between different spans. You can also modify it to use just " " space. Define a contentBetweenTags variable and set it to your desired value. Here is how you can do that:

String contentBetweenTags = ", ";
sb.append(tags + contentBetweenTags);
sb1 = new SpannableStringBuilder();
sb1.append(tags + contentBetweenTags);
sb.setSpan(new ImageSpan(bd), 
           sb.length() - tags.length() - contentBetweenTags.length(), 
           sb.length() - contentBetweenTags.length(), 
           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

第4步)选择正确的空格" 字符

Step 4) Picking the right "space" character

如果您对两个标签之间的边距/间距"不满意,则可以使用许多

Just in case you are not happy with the "margin/spacing" between two tags, you could use one of the many unicode space characters available. They have different widths and you could use any one of them based on your desire / liking.

这是使用unicode \u2002的最终代码和示例屏幕截图:

Here is the final code and sample screenshot using unicode \u2002:

BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());
String contentBetweenTags = ",\u2002";
sb.append(tags + contentBetweenTags);
sb1 = new SpannableStringBuilder();
sb1.append(tags + contentBetweenTags);
sb.setSpan(new ImageSpan(bd), 
           sb.length() - tags.length() - contentBetweenTags.length(), 
           sb.length() - contentBetweenTags.length(), 
           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

这篇关于如何设置跨度textview之间的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆