简体   繁体   English

如何在xml中设置形状的背景?

[英]How to set a shape's background in xml?

I just created a red circle using android shapes:我刚刚使用 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>

This is really cool, but I cannot set the background color of the circle to my color.这真的很酷,但我无法将圆圈的背景颜色设置为我的颜色。 I tried android:background="#FFFFFF" but it always appear to be black in my layout.我试过android:background="#FFFFFF"但它在我的布局中总是看起来是黑色的。 How can I set the background of the above shape?如何设置上述形状的背景?

I think a layer-list might help you:我认为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>
<?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>

I am just adding helpful research as an answer.我只是添加有用的研究作为答案。 Let us suppose you have a shape as the answer described by @GeneBo and you are looking forward to re-using that shape but with a different solid color.让我们假设您有一个shape作为@GeneBo所描述的答案,并且您期待重新使用该形状但具有不同的solid So all you need to do in your widget is:所以你需要在你的小部件中做的是:

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

Ok, how about this?好的,这个怎么样?

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM