49

我刚刚使用 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"了,但在我的布局中它总是看起来是黑色的。如何设置上述形状的背景?

4

4 回答 4

92

我认为 alayer-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>
于 2012-02-13T17:16:39.687 回答
13
<?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>
于 2019-02-06T21:57:14.123 回答
3

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

android:background="@drawable/your_shape_to_reuse"
android:backgroundTint="@color/new_background_color_you_need"
于 2020-07-14T06:18:30.450 回答
-1

好的,这个怎么样?

 <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>
于 2012-02-13T16:58:16.287 回答