Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何向多行可扩展文本添加角半径和填充_Android_Textview_Spannablestring - Fatal编程技术网

Android 如何向多行可扩展文本添加角半径和填充

Android 如何向多行可扩展文本添加角半径和填充,android,textview,spannablestring,Android,Textview,Spannablestring,如何在下面的可扩展文本中添加角半径和填充 public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); setFont(); } public CustomTextView(Context context, AttributeSet attrs) {

如何在下面的可扩展文本中添加角半径和填充

public class CustomTextView extends TextView {
        public CustomTextView(Context context) {
            super(context);
            setFont();
        }
        public CustomTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setFont();
        }
        public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setFont();
        }

        private void setFont() {
            Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");
            setTypeface(font, Typeface.NORMAL);

 Spannable myspan = new SpannableString(getText());
    myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    txtview.setText(myspan);


        }
    }
编辑

你想要这样的东西吗

如果是这样,您可以单独使用以下代码来创建:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#999999"
          android:gravity="center">

    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:gravity="center"
            android:background="@drawable/rounded_corners"/>
</LinearLayout>
我不得不使用不同的颜色,这样你就可以看到它是圆角。您只需使用颜色值即可。我没有TEXT.ttf文件,所以我不知道它会是什么样子。上面的图片当然没有自定义字体。


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@android:color/white"/>
</shape>
刚从上面的答案抄过来,只需要这个。
将此设置为TextView的背景,仅此而已。

上面的设置很简单,但不是我想要的。我在多行文本视图上寻找相同的功能,背景色应该只存在于文本后面。ie:如果文本被推到下一行,则不应显示颜色。酷。基本上它应该是块背景色。颜色、填充和半径应该只存在于文本后面,而不是空白后面。半径应该只存在于空白后面吗?在这种情况下,您需要一个没有填充但带有边距的TextView。半径应该位于文本后面,并且在文本行前后都有边距/填充。图像在整个布局周围有白色背景。我希望它在每行周围和文本内。你能给我看一下吗?我真的记不住了。顺便说一句,如果你想在两行之间留出更多的空间,你可以在TextView元素中使用android:lineSpacingExtra属性。我认为你的问题可以从更多细节中受益匪浅。你能描述一下你想要达到的视觉效果吗?或者,更好的是,通过图片/绘图显示所需的效果?背景色分布在textview的整个版面上,而不仅仅是文字。基本上,如果文本视图单词不能放在那一行中,背景色仍然存在,但我希望背景色只存在于单词后面。
public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/TEXT.ttf");

        TextView textView = (TextView) findViewById(R.id.textView);

        textView.setTypeface(font, Typeface.NORMAL);

        String text = "Text1\nText2\nText3";
        Spannable myspan = new SpannableString(text);
        myspan.setSpan(new BackgroundColorSpan(0xFF757593), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(myspan);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp"/>
    <solid android:color="@android:color/white"/>
</shape>