Cri*_*an 4 android android-radiobutton
我想在一个片段中创建一些dinamically的单选按钮,我只有样式问题.如果我将radiobutton代码放在xml文件中,则会正确应用默认样式,但是当我通过函数创建radiobutton时,我会看到不同的样式!
XML
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:animationCache="false">
<RadioButton
android:text="RadioButton 1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton3" />
<RadioButton
android:text="RadioButton 2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioButton4" />
</RadioGroup>
Run Code Online (Sandbox Code Playgroud)
结果
JAVA CODE
此代码放在片段中的onCreateView中
public void addRadioButton(Context ctx,int num){
RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
for (int i = 1; i <= num; i++) {
RadioButton radioButton = new RadioButton(ctx);
radioButton.setId(1+i);
radioButton.setText("Radio " + radioButton.getId());
radioButton.setTextColor(getResources().getColor(R.color.black));
radioGroup.addView(radioButton);
}
}
Run Code Online (Sandbox Code Playgroud)
结果
正如你所看到的单选按钮有不同的风格,有人可以帮助我,如果可能的话,以编程方式应用默认样式?
Dha*_*tri 14
你必须在drawable或style.xml上创建样式,作为你的要求.
绘制/ null_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)
设置每个按钮以使用它(并使文本居中)像这样(R.drawable.null_selector是选择器XML):
现在,在您的Activity中,您必须实现这样的样式.
RadioButton radioButton = new RadioButton(ctx);
radioButton.setText(Integer.toString(i));
radioButton.setGravity(Gravity.CENTER);
radioButton.setButtonDrawable(R.drawable.null_selector);
Run Code Online (Sandbox Code Playgroud)
我想,这将有助于您在radiobutton中实现自定义样式.
Cri*_*an 5
感谢Dharma,我按照您的建议做了一些更改,然后我解决了!
JAVA代码
public void addRadioButton(Context ctx,int num){
RadioGroup radioGroup= (RadioGroup) alertInflatedView.findViewById(R.id.radiogroup);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.MATCH_PARENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<num; i++){
RadioButton radioButton = new RadioButton(ctx);
radioButton.setId(1+i);
radioButton.setText("Radio"+i);
radioButton.setTextSize(16);
radioButton.setTextColor(getResources().getColor(R.color.black));
radioButton.setButtonDrawable(R.drawable.radio_button_selector);
radioButton.setPadding(80,0,0,0);
radioButton.setGravity(Gravity.CENTER_VERTICAL);
radioButton.setLayoutParams(layoutParams);
radioGroup.addView(radioButton);
}
}
Run Code Online (Sandbox Code Playgroud)
具有选中和未选中按钮图像的XML RADIO BUTTON SELECTOR
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />
<item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
<item android:drawable="@drawable/unchekedradiobutton" /> <!-- default -->
Run Code Online (Sandbox Code Playgroud)
小智 5
使用 Inflater 实例来膨胀自定义布局并轻松获得自定义 Radiobutton
\n\nprivate RadioButton createCustomRadioButton(Context context){\n LayoutInflater inflater = LayoutInflater.from(context);\n View v = inflater.inflate(R.layout.radio_button,null);\n RadioButton radioButton = (RadioButton) v.findViewById(R.id.radio_button);\n radioButton.setText("It Works!");\n\n ((ViewGroup)radioButton.getParent()).removeView(radioButton);\n return radioButton;\n}\nRun Code Online (Sandbox Code Playgroud)\n\nradio_button.xml
\n\n<RadioButton\n android:id="@+id/radio_button"\n style="@style/radio"\n android:background="@drawable/style_line" />\nRun Code Online (Sandbox Code Playgroud)\n\n样式.xml
\n\n<style name="radio">\n <item name="android:layout_width">match_parent</item>\n <item name="android:layout_height">wrap_content</item>\n <item name="android:layout_marginLeft">30dp</item>\n <item name="android:layout_marginRight">30dp</item>\n <item name="android:layout_marginTop">10dp</item>\n <item name="android:layout_marginBottom">10dp</item>\n <item name="android:padding">10dp</item>\n <item name="android:drawablePadding">5dp</item>\n <item name="android:textColor">@color/whiteColor</item>\n <item name="android:textColorHint">@color/hintColor</item>\n <item name="android:editTextColor">@color/whiteColor</item>\n</style>\nRun Code Online (Sandbox Code Playgroud)\n\n作者:乌比拉贾拉 (M\xc3\xa9xico)
\n| 归档时间: |
|
| 查看次数: |
7181 次 |
| 最近记录: |