【问题标题】:how to set two xml file together as background如何将两个xml文件一起设置为背景
【发布时间】:2016-04-15 07:32:44
【问题描述】:

我想要在我的 cardview 周围产生波纹效果和边框,并且我有两个 xml 文件 一个用于边框,另一个用于波纹效果,那么如何在我的卡片视图上设置为背景

这是我的代码

shape.xml(v17)

<?xml version="1.0" encoding="UTF-8"?> 
         <shape xmlns:android="http://schemas.android.com/apk/res/android">
                 <stroke android:width="1dp" android:color="#1B5E20" />
                 <corners android:radius="2dp"/>
                 <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
         </shape>

ripple.xml(v21)

<?xml version="1.0" encoding="utf-8"?>
 <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
     <item
            android:id="@android:id/mask"
            android:drawable="@android:color/white">
     </item>
 </ripple>

这是我想将这两件事放在一起的卡片视图。

cardview.xml

<?xml version="1.0" encoding="utf-8"?>
     <android.support.v7.widget.CardView
            android:layout_marginTop="10dp"
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android">

         <RelativeLayout
                android:background="@drawable/shape"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp">

             <TextView
                    android:textColor="#000"
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Title"
                    android:textSize="20sp" />

         </RelativeLayout>
     </android.support.v7.widget.CardView>

如何将两个文件设置在一起,因为布局中的两个背景不受支持。请帮帮我。

【问题讨论】:

标签: android android-studio


【解决方案1】:

使用图层列表来做。 cardview_style.xml

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape>
        <stroke android:width="1dp" android:color="#1B5E20" />
        <corners android:radius="2dp"/>
        <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
    </shape>
</item>
<item>
    <ripple
        xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:attr/colorControlHighlight">
        <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white"></item>
    </ripple>
</item>

然后将以下文件添加到背景属性。

【讨论】:

  • 我的形状的圆角半径为 100dp,即圆角。如何在波纹的情况下显示相同的内容。波纹看起来像一个矩形。
  • 很好的解决方案。谢谢
【解决方案2】:

就像 Iulian Popescu 说你可以使用 layer-list,这似乎是最好的选择。但是,如果您坚持将它们分开保存,那么您始终可以使用画布将它们合并到您的代码中。 如果您想使用此技术,请使用此主题:

How to merge bitmaps

Converting drawables to bitmaps

【讨论】:

    【解决方案3】:

    使用图层列表创建一个新的 XML,并在其中添加您的形状和波纹

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2012-05-24
      • 2012-03-05
      • 1970-01-01
      • 2013-05-25
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2014-01-25
      相关资源
      最近更新 更多