自定义形状背景 - 剪切背景不起作用

时间:2018-01-16 09:51:48

标签: android selector shape

我希望在弹出列表视图中将自定义形状作为项目的背景图像。但如果我这样做,我会看到以下问题:

enter image description here

我在自定义形状后面看到一个像背景一样的选择器。为什么呢?

我尝试设置android:foreground="@null"因为我认为从任何地方都设置了前景图像,但这并没有什么区别......

观察

style="@style/Widget.AppCompat.Button.Colored"添加到llShortcutParent会导致正确的背景剪辑。为什么?这解决了在我自己的自定义形状背景后面选择背景的简单问题,但我想更进一步用选择器替换形状背景,但这导致了另一个问题:

如果我使用选择器,这个技巧不再起作用,因为选择器根本不起作用,只绘制默认状态背景

所以我卡住了,我要么有一个工作选择器(如果我删除了样式)但是在我自己的选择器后面有一个默认选择器,比如背景,或者我有一个没有选择器的好看的视图......

解决方案

  • 简单的解决方案,如果没有使用选择器:

    style="@style/Widget.AppCompat.Button.Colored"解决了这个问题

  • 使用选择器作为backgorund的解决方案:

    ??? =>还没有人找到......

代码

我的布局:

<LinearLayout
    android:id="@+id/llShortcutParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shortcut_shape"
    android:duplicateParentState="true"
    android:elevation="20dp"
    android:orientation="horizontal">

</LinearLayout>

我的自定义形状如下(shortcut_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFF"/>
    <stroke
        android:width="2dp"
        android:color="#FFF"/>
    <padding
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp"/>
    <corners android:radius="50dp"/>
</shape>

2 个答案:

答案 0 :(得分:0)

创建一个新的drawable并应用为

android:foreground="@drawable/circle.xml"

<强> circle.xml

<?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/colorShadow"
        android:radius="38dp" />

答案 1 :(得分:0)

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"      android:shape="rectangle" >
 <corners android:radius="14dp"      />
 <gradient
 android:angle="45"
 android:centerX="35%"
 android:centerColor="#A83CA8"
 android:startColor="#A83CA8"
 android:endColor="#A83CA8"
 android:type="linear"
 />
 <padding
 android:left="0dp"
 android:top="0dp"
 android:right="0dp"
 android:bottom="0dp"
 />
 <size
 android:width="270dp"
 android:height="60dp"
 />
 <stroke
 android:width="3dp"
 android:color="#A83CA8"
 />
 </shape>

应用此代码

相关问题