Android 安卓形状背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11310362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:52:04  来源:igfitidea点击:

Android shape background

androidbackgroundshape

提问by Dorin Rusu

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.

是否可以在 xml 中绘制一个形状,并使用 png 作为该形状的背景?我已经有了这个形状(它是一个带圆角的正方形),我想给那个正方形加上背景。

回答by Drup Desai

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

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

回答by Padma Kumar

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