【问题标题】:How to set a shape's background in xml?如何在xml中设置形状的背景?
【发布时间】:2012-03-05 01:58:30
【问题描述】:

我刚刚使用 android 形状创建了一个红色圆圈:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="4"
    android:shape="ring"
    android:thicknessRatio="9"
    android:useLevel="false" >

     <solid android:color="#FF0000" />

    <size
        android:height="48dip"
        android:width="48dip" />

</shape>

这真的很酷,但我无法将圆圈的背景颜色设置为我的颜色。我试过android:background="#FFFFFF",但在我的布局中它总是看起来是黑色的。如何设置上述形状的背景?

【问题讨论】:

    标签: android colors background shape android-drawable


    【解决方案1】:

    我认为layer-list 可能会对您有所帮助:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle" >
                <solid android:color="#ffffff" />
            </shape>
        </item>
        <item>
            <shape
                android:innerRadiusRatio="4"
                android:shape="ring"
                android:thicknessRatio="9"
                android:useLevel="false" >
                <solid android:color="#FF0000" />
                <size
                    android:height="48dip"
                    android:width="48dip" />
            </shape>
        </item>
    
    </layer-list>
    

    【讨论】:

      【解决方案2】:
      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
      
          <corners android:radius="12dp" />
          <solid android:color="#ffffff" />
          <stroke
              android:width="1dp"
              android:color="@android:color/black" />
      
      </shape>
      

      【讨论】:

        【解决方案3】:

        我只是添加有用的研究作为答案。让我们假设您有一个shape 作为@GeneBo 描述的答案,并且您期待重新使用该形状,但使用不同的solid 颜色。所以你需要在你的小部件中做的就是:

        android:background="@drawable/your_shape_to_reuse"
        android:backgroundTint="@color/new_background_color_you_need"
        

        【讨论】:

        • 您可能还想添加android:backgroundTintMode="screen" 或其他模式。
        【解决方案4】:

        好的,这个怎么样?

         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            >
        
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:background="#FFFFFF">
        <TextView android:text="Foo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:gravity="center"
            android:background="@drawable/red_circle"/>
            </LinearLayout>
        
        
        </LinearLayout>
        

        【讨论】:

        • 我用的是TextView,可惜我需要在里面放文字
        • 我刚刚发布了一个类似作品的更新,但不幸的是,圆圈的顶部和底部都被切断了。我必须再玩一会儿才能让它完全扩展。
        猜你喜欢
        • 2014-08-22
        • 2012-05-24
        • 1970-01-01
        • 2014-04-21
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多