【问题标题】:Custom TextView background自定义 TextView 背景
【发布时间】:2015-11-11 21:21:43
【问题描述】:

我想创建带有自定义背景的 TextView,例如

但不幸的是,我不知道该怎么做。也许有人知道如何在 xml 中创建背景(而不是在 java 代码中)?提前谢谢你

【问题讨论】:

标签: android xml textview android-custom-view


【解决方案1】:

在代码中使用 setBackgroundResource(int resourceId) 或在布局中使用 android:background

Java:

mTextView.setBackgroundResource(R.drawable.yourDrawable);

布局:

<TextView
    android:background="@drawable/yourDrawable"
    ...
    />

【讨论】:

    【解决方案2】:

    创建这样的布局。您必须为此使用自定义图像。 你的代码在这里

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="your_custom_image"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="text_goes_here"
            android:textColor="specify_color" />
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 2017-04-14
      • 1970-01-01
      • 2011-10-30
      • 2010-10-16
      • 2018-07-13
      相关资源
      最近更新 更多