Android形状背景

Android形状背景,android,background,shape,Android,Background,Shape,是否可以用xml绘制形状,并使用png作为该形状的背景?我已经有了这个形状(它是一个带圆角的正方形),我想给这个正方形加一个背景。是的,你可以使用任何形状文件作为任何视图的背景。此示例创建圆形背景,形状周围有白色和黑色边框 样本: rounded_corner.xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <co

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

是的,你可以使用任何形状文件作为任何视图的背景。此示例创建圆形背景,形状周围有白色和黑色边框

样本:

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>

你可以把它当作

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

//试试这种方法这会对你有帮助

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


我们可以放置任何可绘制的图像而不是白色吗?不,你不能在形状xml中添加任何可绘制的图像,它只需要color.OP wants.png图像作为背景。是的,最后我“作弊了”,因为我已经在Photoshop中绘制了形状并手动添加了背景,但我仍然希望有这样的想法。谢谢你的回答!形状的背景可以作为参考吗?在我的空白
Xamarin.android
项目中,
Resources
下没有名为
drawable
的文件夹。如果我把这个形状放在
mipmap-anydpi-v26
文件夹中,那么我可以访问它并在模拟器中正确使用它,但是应用程序在一个真实的分区上崩溃,并出现以下错误:
Binary XML file line#1:错误膨胀类
。你知道为什么会这样吗?读我的帖子