android - 为每个 GridLayout 正方形添加分隔线和波纹效果
问题描述
我正在使用 RecyclerView + GridLayout(3 列)。现在,为了让网格的每个方格更“响应”,我希望每个方格都显示某种分隔线,并且用户点击的每个方格内都会产生涟漪效应。
编辑:我添加了android:foreground="?attr/selectableItemBackground"
,但没有任何反应。这是现在的单项 xml 代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dip" >
<ImageView
android:foreground="?attr/selectableItemBackground"
android:id="@+id/icon"
android:layout_width="128dp"
android:layout_height="118dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="12dp"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher_background" />
</RelativeLayout>
这是修复后当前的工作方式,添加后:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
解决方案
如果您希望对其进行自定义,您可以像这样创建一个可绘制的波纹:
波纹.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimaryDark">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryLight" />
<!--<corners android:radius="@dimen/button_radius_large" />-->
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/background"
android:startColor="@color/background"
android:type="linear" />
<!--<corners android:radius="10dp" />-->
</shape>
</item>
并在您的布局中使用它,如下所示:
android:clickable="true"
android:background="@drawable/ripple"
或者你可以简单地这样做:
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
推荐阅读
- angular - 查看子项不适用于 Modal -Angular 内的 Element Ref
- flutter - Flutter getx无限滚动刷新listview每次结束滚动
- html - 新标签中的角度显示图像
- windows - Visual Studio:删除“我的代码片段”文件夹是否安全?
- java - 使用 JUnit 5 的并发 Spring Boot 测试正在启动多个 JVM
- r - 从闪亮的事件反应生成数据框
- java - 尝试实现 dijkstra 的算法(在我无法完全修复的线程中遇到异常)
- c - 在显示之前将数据传递给 Glade UI
- c++ - 我应该只向构造函数和移动运算符编写 noexcept 吗?
- angular - 为什么在具有 formGroup.errors 时没有 mat-error 的 Angular 表单有效?