【问题标题】:Add Rounded corners to StateSelected Background of TextView using XML使用 XML 将圆角添加到 TextView 的 StateSelected 背景
【发布时间】:2016-01-28 07:54:11
【问题描述】:

我有RecyclerView,其中包含一些TextViews。我已将RecyclerView 的背景设置为以下recycler_view_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#000000" />
    <corners android:topLeftRadius="@dimen/player_category_radius" android:topRightRadius="@dimen/player_category_radius"
        android:bottomLeftRadius="@dimen/player_category_radius" android:bottomRightRadius="@dimen/player_category_radius"/>
    <stroke android:color="#D3D3D3" android:width="1dp" />
</shape>

而且效果很好,我的RecyclerView 得到了圆角。

这就是问题所在,当我尝试将背景选择器添加到任一 TextViews 时,它们不会显示圆角。这是xml 的每个项目category_item_selector.xml 的背景。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/controlbar_gradient">
        <shape>
            <corners android:topLeftRadius="@dimen/player_category_radius" android:topRightRadius="@dimen/player_category_radius"
                android:bottomLeftRadius="@dimen/player_category_radius" android:bottomRightRadius="@dimen/player_category_radius"/>
            <stroke android:color="#D3D3D3" android:width="1dp" />
        </shape>
    </item>
</selector>

我在RecyclerView.Adapter.ViewHolderOnClickListener 中以编程方式将TextView's selected 设置为true(选择器工作正常,否则所选项目的背景不会是红色)。

这是来自应用程序的 sn-ps。

RecyclerView 有圆角,但TextView 的背景在上面绘制。 So when the selected View is on Top or Bottom.

圆角不再可见,尽管当我在选定视图上添加圆角时它不应该可见。

在搜索此内容时,我不断得到类似的解决方案。根据this,我是正确的。

【问题讨论】:

  • 这个选择器不起作用,因为它只有一个状态(你也需要一个默认值)并且你刚刚为此定义了可绘制对象。内部形状将被忽略.....
  • @Opiatefuchs 我不需要 state_selected="false" 的圆角。所以它在那个级别上工作得很好。至于已经定义的drawable,请在我指出自己错误的地方查看我的答案。

标签: android xml android-layout xml-drawable


【解决方案1】:

所以在这里发布这个问题时。我修改并发现了我的错误。就是这样。

在我的 category_item_selector.xml 中,我将背景设置为 @drawable/controlbar_gradient,其定义为

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient

        android:angle="90"
        android:type="linear"
        android:startColor="#7e0809"
        android:endColor=  "#fa0000" />

</shape>

稍后在我的 category_item_selector.xml 中,我再次添加了shape。所以编译器采用了它找到的第一个shape 并渲染了它。所以我所要做的就是将category_item_selector.xml 更改为

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <shape>
            <gradient

                android:angle="90"
                android:type="linear"
                android:startColor="#7e0809"
                android:endColor=  "#fa0000" />

            <corners android:topLeftRadius="@dimen/player_category_radius" android:topRightRadius="@dimen/player_category_radius"
                android:bottomLeftRadius="@dimen/player_category_radius" android:bottomRightRadius="@dimen/player_category_radius"/>
            <stroke android:color="#D3D3D3" android:width="1dp" />
        </shape>
    </item>
</selector>

【讨论】:

    【解决方案2】:

    你还有另一种方法可以做到这一点......

    为您的 RecyclerView 设置背景后为您的 RecyclerView 项目设置背景

    喜欢这个

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_most_trending"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/recyclerView_background"
        >
    
    </android.support.v7.widget.RecyclerView>
    <?xml version="1.0" encoding="utf-8"?>
    

    为 RecyclerView 项目设置背景

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@drawable/recyclerView_item_bg">
        <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="right"
          android:padding="10dp"
          />
     </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 2013-06-28
      • 1970-01-01
      • 2020-10-28
      相关资源
      最近更新 更多