如何让 Android 上的 WIDGET 上的按钮在单击时显示 Material 波纹?
<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>
样式.xml
<style name="MyStyle" parent="Base.Widget.AppCompat.Button.Borderless">
<item name="android:layout_width">@dimen/button_height</item>
<item name="android:layout_height">@dimen/button_height</item>
<item name="android:minHeight">0dp</item>
<item name="android:padding">8dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:textAllCaps">false</item>
<item name="android:drawablePadding">0dp</item>
<item name="android:background">@color/white_pressed</item>
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
<item name="android:orientation">vertical</item>
</style>
上面显示了我如何使用线性布局来表示小部件的可单击按钮。它还包括样式详细信息。
我尝试添加该行
android:background="?attr/selectableItemBackground"
到 LinearLayout 但在小部件上出现错误,指出“加载小部件时出现问题”。如果我将属性移至样式本身,也会出现此问题。
看起来我想要实现的目标是可能的,因为 Android 上的 Google Drive 小部件具有此效果。这是如何完成的?
谢谢!
最佳答案
将这些属性添加到您的 LinearLayout
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
注意:这在 Lollipop 下不起作用。
关于java - Android 小部件点击时波纹背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50868182/