【问题标题】:How to create a custom TextView Background Android?如何创建自定义 TextView 背景 Android?
【发布时间】:2014-03-08 19:45:30
【问题描述】:

我想为我的 TextView 创建一个背景,使其看起来完全像这个(黄色框)。

创建图像后,如何设置电视的背景?

谢谢!

【问题讨论】:

    标签: android textview


    【解决方案1】:

    为此,您创建了简单的图像。然后转换成九个补丁here

    在为该可绘制对象设置背景之后

    九个补丁图像示例

    【讨论】:

      【解决方案2】:

      或者你可以创建一个可绘制的形状。

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
      
      <solid android:color="@color/Yellow" />
      <stroke android:width="@dimen/" android:color="@color/"/> 
      <corners android:radius="@dimen/"/>
      <gradient
          android:startColor="@color/"
          android:centerColor="@color/"
          android:endColor="@color/"
          android:centerX="0.5"
          android:centerY="0.5"
          android:gradientRadius="100"
          android:type="linear" />
      </shape>
      

      【讨论】:

        【解决方案3】:

        也可以使用 shape 属性来完成。

        我使用以下内容为自己创建了一个类似的。

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:padding="10dp"
          android:shape="rectangle" >
        
          <solid android:color="#FFFFFF" /> //This should be the yellow color you want
        
          <corners android:radius="5dp" /> //for rounded corners
        
           <stroke
            android:width="2dp"
            android:color="@color/color_dark_blue" /> //for border if you wish any
        
        </shape>
        

        在 drawable 中将其创建为 xml 文件(比如 myTextViewBg.xml)。

        然后将其作为背景添加到您的 TextView 中,

        yourTextView:background="@drawable/myTextViewBg.xml"
        

        【讨论】:

          【解决方案4】:

          您可以设置文本视图的背景 android:background="@drawable/myResouce" 或使用java mTextView.setBackgroundResource(R.drawable.myResouce);

          但在您的情况下,创建一个相对布局,然后在其中放置一个 imageView 和 Textview 并根据您的视图进行调整。

          imageView 中使用您在问题中的图片,希望文字会发生变化。在textView 中使用可变文本。你也可以务实地改变它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-08-06
            • 1970-01-01
            • 1970-01-01
            • 2017-04-14
            • 2015-06-13
            • 2020-09-27
            • 2017-08-18
            相关资源
            最近更新 更多