Android 设置以编程方式添加的视图的样式

Android 设置以编程方式添加的视图的样式,android,android-styles,programmatically-created,Android,Android Styles,Programmatically Created,在我的代码中,我以编程方式向布局中添加输入元素,如单选按钮、复选框等。 问题是,这些元素的样式不是通过xml添加radioButton时得到的默认样式。(它看起来真的很白,在白色的应用程序背景上几乎是透明的。有点像是透明的) 此外,我添加的EditText元素具有相同的样式,如果在其中键入内容,则文本太大,与文本行有点重叠。 所以我想这一切都归结为以某种方式赋予这些元素它们的默认样式,就像它们通过xml定义时的样子一样 我的代码示例如下所示: RadioGroup radioGroup = ne

在我的代码中,我以编程方式向布局中添加输入元素,如单选按钮、复选框等。 问题是,这些元素的样式不是通过xml添加radioButton时得到的默认样式。(它看起来真的很白,在白色的应用程序背景上几乎是透明的。有点像是透明的) 此外,我添加的EditText元素具有相同的样式,如果在其中键入内容,则文本太大,与文本行有点重叠。 所以我想这一切都归结为以某种方式赋予这些元素它们的默认样式,就像它们通过xml定义时的样子一样

我的代码示例如下所示:

RadioGroup radioGroup = new RadioGroup(mContext);
    radioGroup.setLayoutParams(fullWidthWrapHeight);

    for (int i = 0; i < arg0.getOptions().size(); i++){
        RadioButton radioButton = new RadioButton(mContext, null);
        radioButton.setPadding(padding16dp , padding8dp, padding16dp, padding8dp);
        radioButton.setText(arg0.getOptions().get(i).getText());
        radioButton.setLayoutParams(wrapBoth);
        radioButton.setGravity(Gravity.CENTER_HORIZONTAL);

        radioButton.setTextAppearance(mContext, R.style.Default_Text);
        radioGroup.addView(radioButton);
    }
RadioGroup-RadioGroup=新的RadioGroup(mContext);
射线组setLayoutParams(全宽范围);
对于(int i=0;i

我的目标API lvl是21(棒棒糖)

您可以将
styles.xml
中定义的样式作为
视图
构造函数的参数传递。考虑到你的例子,你必须打电话:

RadioButton radioButton = new RadioButton(mContext, null, R.attr.radioButtonStyle);
然后在
attrs.xml

<attr name="radioButtonStyle" format="reference" />

YourRadioButtonStyle
是在
styles.xml

中定义的自定义单选按钮样式。对我来说,正是通过这种方式成功地解决了这个问题:

<attr name="radioButtonStyle" format="reference" />
Activity.java

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

radio_butt.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="@color/colorWhite"
    android:theme="@style/MyRadioButtonStyle"/>
<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="colorControlNormal">@color/colorAlfaWhite</item>
    <item name="colorControlActivated">@color/colorWhite</item>
</style>

slyles.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    android:textColor="@color/colorWhite"
    android:theme="@style/MyRadioButtonStyle"/>
<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="colorControlNormal">@color/colorAlfaWhite</item>
    <item name="colorControlActivated">@color/colorWhite</item>
</style>

@颜色/颜色/白色
@彩色/彩色白色

是否也可以通过某种方式传递默认的单选按钮样式,或者我必须创建一个以默认样式为父级的新样式吗?您必须定义以
@android:style/Widget.CompoundButton.RadioButton
为父级的
YourRadioButton样式
,并设置一些字段,以便传递您应该能够使用的默认RadioButton样式
com.android.internal.R.attr.radioButtonStyle
而不是
R.attr.radioButtonStyle
如果我这样做,文本会与复选框图像重叠。。。知道为什么会这样吗?也许这和