【问题标题】:How to programatically set the colour of a RadioButton Android Circle如何以编程方式设置 RadioButton Android Circle 的颜色
【发布时间】:2016-11-07 19:17:00
【问题描述】:

我想更改RadioButton的实际圈子。

我查看了整个 StackOverflow,但似乎没有任何效果。

我正在使用 API 17,无法使用 ColorStateList。自定义可绘制对象似乎让我们的外观和感觉变得混乱。

我想要与更改按钮提示相同的效果,但我需要以编程方式进行。

我确定我必须遗漏一些非常简单的东西!

【问题讨论】:

标签: android android-radiobutton


【解决方案1】:

如何以编程方式设置 Android RadioButton 的颜色

这真的很不具体,所以这里是我发现的最相关的:

RadioButton rad;//initialize first!
//You can set the background color
rad.setBackgroundColor(Color.BLUE);
//Text color
rad.setTextColor(Color.WHITE);
//or highlight color
rad.setHighlightColor(Color.GREEN);

高亮颜色是按住 RadioButton 时出现的颜色(默认为黄色)

编辑:

在单选按钮的初始化中,改为AppCompatRadioButton

AppCompatRadioButton rad = ....
rad.setHighlightColor(Color.GREEN);

重做自:https://stackoverflow.com/a/32472971/6296561

编辑

试试这个:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    </RadioGroup>

</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);

        /**
         * First Radio Button
         */
        RadioButton RB1= (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
        RB1.setText("RB1");
        radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout

        /**
         * Second Radio Button
         */
        RadioButton RB2 = (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
        RB2.setText("RB2");
        radioGroup.addView(RB2);//add the radiobutton to the radiogroup defined in the layout
    }
}

custom_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:buttonTint="@color/colorPrimary"
    android:text="">
<!-- leave empty -->
</RadioButton>

注意: buttonTint仅适用于 API 21+。 (未经测试)您可以将 RadioButton 更改为 AppCompatRadioButton。 (它未经测试,所以我不确定它是否适用于 api 20 及更低版本)

<?xml version="1.0" encoding="utf-8"?>
<AppCompatRadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:buttonTint="@color/colorPrimary"
    android:text="">
<!-- leave empty -->
</AppCompatRadioButton>

Documentation about AppCompatRadioButton

如果您使用 AppCompatRadioButton,我认为您还必须使用 AppCompatRadioGroup 并将 ACRB 的创建编辑为:

AppCompatRadioButton RB1 = (AppCompatRadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
RB1.setText("RB1");
radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout

【讨论】:

  • 对不起,你说得对,我没有具体说明。我编辑了它。我想改变圆圈的颜色,所以很遗憾你的答案不起作用
  • 更新了问题。这对你有帮助吗?
  • @color 可以换成#和我认为的颜色代码
  • 什么都不做,很遗憾没有效果
【解决方案2】:

可以使用您自己的自定义图像来更改 RadioButton 圆圈的颜色。 假设您有两个圆形图像用于选中和未选中即。选中的单选按钮和未选中的单选按钮。现在制作一个 xml(custom_radio_button) 可绘制如下:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
<item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />

我相信您正在使用

创建单选按钮
  RadioButton rb = new RadionButton(context);
  radioButton.setLayoutParams(new 
  ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
  ViewGroup.LayoutParams.WRAP_CONTENT));
  radioButton.setTextColor(ContextCompat.
  getColor(context,R.color.royal_blue));

 radioButton.setButtonDrawable(R.drawable.custom_radio_button);//this will change the default circles

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 2015-07-01
    • 2015-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多