如何更改圆角文本视图或具有透明背景的按钮的颜色,像这样
我希望再次单击按钮时取消选择,而不仅仅是选择
如何更改圆角文本视图或具有透明背景的按钮的颜色,像这样
我希望再次单击按钮时取消选择,而不仅仅是选择
在可绘制文件夹 bg.xml 中添加 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<stroke
android:height="1.0dip"
android:width="1.0dip"
android:color="#ffee82ee" />
<solid android:color="@android:color/transparent"/>
<corners android:radius="7dp"/>
</shape>
并在布局
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:textColor="#ffee82ee"/>
它会工作..
您可以使用具有多种状态的选择器作为背景和文本颜色的可绘制对象。
<Button
android:id="@+id/button1"
android:background="@drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
可绘制的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/numpad_button_bg_normal"></item>
</selector>
你有不同的选择:
MaterialButton
中的style 和,属性即可。Widget.MaterialComponents.Button.OutlinedButton
app:cornerRadius
app:strokeColor
就像是:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="BUTTON"
app:strokeColor="@color/myColor"
app:cornerRadius="16dp"
../>
要实现按钮的选择,您可以使用MaterialButtonToggleGroup
.
就像是:
<com.google.android.material.button.MaterialButtonToggleGroup
app:singleSelection="true"
...>
<com.google.android.material.button.MaterialButton
.../>
</com.google.android.material.button.MaterialButtonToggleGroup>
Chip
就像是:
<com.google.android.material.chip.Chip
style="@style/Widget.MaterialComponents.Chip.Entry"
app:chipCornerRadius="16dp"
android:text="Chip"
.../>
您必须使用选择器创建背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/likeactivepressed" />
<item android:state_pressed="true" android:drawable="@drawable/likeinitialpressed"/>
<item android:state_checked="true" android:drawable="@drawable/likeon"/>
<item android:drawable="@drawable/likeinitial"/>
</selector>
然后将背景设置为按钮:
android:background="@drawable/like_button"