Android 如何在线性布局上产生涟漪效果,而不覆盖其子级上的背景色?
我有一个像这样的线性布局。 我希望每一行都可以点击。行的Android 如何在线性布局上产生涟漪效果,而不覆盖其子级上的背景色?,android,android-layout,rippledrawable,Android,Android Layout,Rippledrawable,我有一个像这样的线性布局。 我希望每一行都可以点击。行的LinearLayout代码如下所示: <LinearLayout style="@style/home_menu_item_container" android:id="@+id/home_menu_acronyms_button" > <ImageView style="@style/home_menu_item_left
LinearLayout
代码如下所示:
<LinearLayout
style="@style/home_menu_item_container"
android:id="@+id/home_menu_acronyms_button"
>
<ImageView
style="@style/home_menu_item_left"
android:background="@color/greyLight"
/>
<TextView
style="@style/home_menu_item_right"
android:text="@string/home_menu_option_2"
android:background="@color/grey"
/>
</LinearLayout>
如何添加扩展到整行(父视图)而不仅仅是行中的一个子视图的连锁反应?这里棘手的部分是让涟漪覆盖两种颜色的行。这有点复杂,但我认为没有其他方法
您需要将ImageView放入一个
列表视图
,这样每个ImageView都是一个列表项
,然后您可以设置涟漪
,但您还需要设置drawSelectorOnTop=“true”
,否则它目前无法正常工作,我发现最简单的方法是在可绘制资源中定义一个
,然后将线性布局的背景设置为该可绘制资源
定义您的drawable-v21/item_selector.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/your_background_color">
<item android:id="@android:id/mask"
<!--color here doesn't matter-->
android:drawable="@android:color/white" />
</ripple>
此外,如果没有自己的背景色,则根本不需要定义项选择器。您可以简单地将背景定义为android:background=“?android:attr/selectableItemBackground”
,用于LinearLayout
另一个选项是使涟漪的背景色透明。这样只能看到涟漪。ripple的xml文件(位于drawable-v21/文件夹中)如下所示:
注意如果您支持棒棒糖制作前的设备,则需要在可绘制/文件夹中有一个同名的虚拟文件。对于该文件,空选择器已足够。请记住,那些旧设备不会产生涟漪。上面的答案不适用于我,
以下是我的解决方案:
ripple.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
>
...put het your design
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ripple" />
</FrameLayout>
…把他放在你的设计上
我也面对了这个问题,最终找到了一个简单的解决方案
在线性布局中,只需添加安卓:clickable=“true”;
并将具有ur涟漪效果的背景设置为
android:background=“@drawable/ripple_effect”
代码:
<LinearLayout
android:clickable="true"
android:background="@drawable/ripple_effect"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add your child views here -->
</LinearLayout>
在drawable中添加ripple_effect.xml
ripple_effect.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f87d05c2"
tools:targetApi="lollipop">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f87d05c2" />
</shape>
</item>
</ripple>
谢谢!假设一个ListItem可以同时包含ImageView和TextView,对吗?是的,没问题,您可以按照您想要的方式定义项目布局,。。。我认为还有一个预定义的功能可以将图像添加到列表项的左侧和/或右侧。它应该类似于android:drawableLeft/android:drawableRightMission.:)谢谢你的回答。@Zac第二个答案不应该被接受吗?
<LinearLayout
android:clickable="true"
android:background="@drawable/ripple_effect"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- Add your child views here -->
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f87d05c2"
tools:targetApi="lollipop">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f87d05c2" />
</shape>
</item>
</ripple>
<RelativeLayout
android:id="@+id/rll_privacy_policy"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/txt_privacy_policy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/layout_margin_16"
android:text="@string/privacy_policy"
android:layout_centerVertical="true"
android:textColor="@color/link_acc"
android:textSize="@dimen/txt_title_20" />
</RelativeLayout>