Java 单击Android小部件时会出现背景波纹
我如何在Android上的小部件上获得一个按钮,以便在单击时显示材质涟漪Java 单击Android小部件时会出现背景波纹,java,android,android-layout,kotlin,widget,Java,Android,Android Layout,Kotlin,Widget,我如何在Android上的小部件上获得一个按钮,以便在单击时显示材质涟漪 <LinearLayout android:id="@+id/button_power" style="@style/MyStyle"> <ImageView android:id="@+id/image_view_power" android:layout_width="@dimen/widget_button_image_size"
<LinearLayout
android:id="@+id/button_power"
style="@style/MyStyle">
<ImageView
android:id="@+id/image_view_power"
android:layout_width="@dimen/widget_button_image_size"
android:layout_height="@dimen/widget_button_image_size"
android:layout_gravity="center_horizontal"
android:contentDescription="power"
android:src="@drawable/baseline_power_settings_new_24"
android:tint="@color/primary_text" />
<TextView
android:id="@+id/text_view_power"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:textColor="@color/primary_text"
android:textSize="@dimen/widget_button_text_size" />
</LinearLayout>
到LinearLayout,但小部件上出现错误,说明“加载小部件时出现问题”。如果将属性移动到样式本身,也会出现问题
看起来我想要实现的是可能的,因为Android上的GoogleDrive小部件具有这种效果。这是怎么做到的
谢谢大家! 将这些属性添加到线性布局中
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
注意:这在棒棒糖下面不起作用 您可以使涟漪可绘制:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</ripple>
然后将此可绘制设置为布局的背景遵循此答案,您的问题将得到解决。
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</ripple>