gpt4 book ai didi

android - 添加 selectableItemBackground 和着色背景

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:14 26 4
gpt4 key购买 nike

我在 CardView 上添加了 selectableItemBackground,它显示在 recyclerview 上。每个 CardView 显示文本和图像。每张卡片都可以选择,为了在点击时添加涟漪效果,我添加了:

android:background="?attr/selectableItemBackground"

问题是现在我无法设置背景颜色。这两行一起显示错误:

android:background="?attr/selectableItemBackground"
android:background="#FFFFFF"

如何为布局背景着色并使用 selectableItemBackground 添​​加波纹效果。

完整的card_view.xml代码:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
card_view:cardCornerRadius="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:orientation="vertical">
<!--CANT ADD THIS: android:background="#FFFFFF"-->

<TextView
android:id="@+id/singleTextLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="3dp"
android:text="Test"
android:textSize="30dp" />

<ImageView
android:id="@+id/singleImage"
android:layout_width="fill_parent"
android:layout_height="125dp"
android:layout_gravity="center_horizontal"
android:layout_margin="3dp" />

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

问题是,在结合 ?attr/selectableItemBackground 以获得涟漪效果时,我无法将背景着色为白色,然后它看起来像这样: enter image description here

最佳答案

你有两个选择:

1:添加父线性布局:

父布局有背景色和子布局具有链式 react

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:orientation="vertical">

<TextView
android:id="@+id/singleTextLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="3dp"
android:text="Test"
android:textSize="30dp" />

<ImageView
android:id="@+id/singleImage"
android:layout_width="fill_parent"
android:layout_height="125dp"
android:layout_gravity="center_horizontal"
android:layout_margin="3dp" />


</LinearLayout>
</LinearLayout>

2:光电:创建绘制资源:bg_ripple.xml

文件夹drawable中的一个

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/gray"/>
<item android:drawable="@color/white"/>
</selector>

另一个在文件夹drawable-v21

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/gray">
<item android:drawable="@color/white" />
</ripple>

然后将此资源设置为布局的背景:

android:background="@drawable/bg_ripple"

关于android - 添加 selectableItemBackground 和着色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33808180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com