如何以编程设置的单选按钮的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