android radiobutton 按钮颜色,如何以编程方式设置RadioButton Android Circle的颜色

如何以编程设置的单选按钮的Android

那可真是非特异性的颜色,所以这里是最相关的,我发现:

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时出现的颜色(默认为黄色ish)

编辑:

在单选按钮的初始化,称其为AppCompatRadioButton代替

AppCompatRadioButton rad = ....

rad.setHighlightColor(Color.GREEN);

编辑

试试这个:

activity_main.xml中

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

android:id="@+id/radioGroup"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:buttonTint="@color/colorPrimary"

android:text="">

注:buttonTint只能在API 21+。 (未测试),您可以将RadioButton更改为AppCompatRadioButton。 (这是未经测试,所以我不知道这是否适用于API 20,下)

android:layout_width="match_parent"

android:layout_height="match_parent"

android:buttonTint="@color/colorPrimary"

android:text="">

如果使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值