0

如何更改圆角文本视图或具有透明背景的按钮的颜色,像这样图片

我希望再次单击按钮时取消选择,而不仅仅是选择

4

4 回答 4

3

在可绘制文件夹 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"/>

它会工作..

于 2018-10-14T08:04:04.753 回答
1

您可以使用具有多种状态的选择器作为背景和文本颜色的可绘制对象。

  <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>
于 2018-10-14T08:05:16.657 回答
0

你有不同的选择:

就像是:

    <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>

就像是:

    <com.google.android.material.chip.Chip
        style="@style/Widget.MaterialComponents.Chip.Entry"
        app:chipCornerRadius="16dp"
        android:text="Chip"
        .../>

在此处输入图像描述

于 2019-09-03T22:37:58.977 回答
0

您必须使用选择器创建背景

<?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"
于 2018-10-14T08:06:50.867 回答