【问题标题】:Android shape background安卓形状背景
【发布时间】:2012-07-03 11:55:08
【问题描述】:

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

【问题讨论】:

    标签: android background shape


    【解决方案1】:

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

    示例:

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

    【讨论】:

    • 我们可以放置任何可绘制对象(即图像)而不是白色吗?
    • 不,您不能在形状 xml 中添加任何可绘制对象,它只需要颜色。
    • OP 希望 .png 图像在背景中。
    • 是的,最后我“作弊”了,因为我已经在 Photoshop 中绘制了形状并手动添加了背景,但我仍然想要一个想法。感谢您的回答!
    • 形状的背景可以作为参考吗?喜欢 -> android:color="?btn_background"
    【解决方案2】:

    //试试这个方法对你有帮助

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

    【讨论】:

    • 在我空白的Xamarin.Android 项目中,Resources 下没有名为drawable 的文件夹。如果我将形状放在mipmap-anydpi-v26 文件夹中,那么我可以访问它并在模拟器中正确使用它,但应用程序在真实设备上崩溃并出现以下错误:Binary XML file line #1: Error inflating class。关于为什么会发生这种情况的任何想法?阅读我的帖子here
    猜你喜欢
    • 2020-04-18
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多