Android shape background

2019-03-14 05:35发布

Is it possible to draw a shape in xml, and use a png as the background to that shape? I already have the shape (it's a square with rounded corners), and i would like to put a background to that square.

2条回答
混吃等死
2楼-- · 2019-03-14 06:10

Yes you can use any shape file as background for any view. This sample create rounded background with white color and black border around the shape.

Sample :

rounded_corner.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

    <stroke
        android:width="0.5dp"
        android:color="@color/color_grey" />

    <solid android:color="@color/color_white" />

</shape>

u can use this as,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/rounded_corner"
    android:orientation="vertical" >
查看更多
乱世女痞
3楼-- · 2019-03-14 06:19

//try this way this will help you

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner"
        android:padding="2dp"
        android:orientation="vertical" >
<ImageView 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" 
    android:background="@drawable/yourdrawable />
</LinearLayout>
查看更多
登录 后发表回答