简体   繁体   English

在Android中以编程方式添加到RadioGroup时,RadioButtons具有不同的样式

[英]RadioButtons have a different style when added to a RadioGroup programmatically in Android

I'm building a form in an Android app. 我正在Android应用中构建表单。

The form has several fields where two components are RadioGroups. 该表单有几个字段,其中两个组件是RadioGroups。 The first group inclusive its buttons is completely defined in the layout file for the activity. 包含其按钮的第一组完全在活动的布局文件中定义。 For the second group only the RadioGroup element is defined in the layout file where as the RadioButtons get added to the group during run time. 对于第二组,仅在布局文件中定义RadioGroup元素,其中RadioButtons在运行时被添加到组中。

As you can see in the image below I got some styling issues. 正如您在下图中看到的,我遇到了一些样式问题。 The radio buttons in the second group look different than the buttons in the first group. 第二组中的单选按钮看起来与第一组中的按钮不同。 The button image and the text color for the second group are different. 按钮图像和第二组的文本颜色不同。 Beside the orientation for the buttons both RadioGroups are configured with the same attributes. 除了按钮的方向,两个RadioGroup都配置了相同的属性。 When I add the buttons of the second group directly in the layout file then their look as equal to the first group. 当我在布局文件中直接添加第二组的按钮时,它们的外观与第一组相同。

在此输入图像描述

Layout file. 布局文件。

<RadioGroup
    android:id="@+id/radio_gender"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="4dp"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/radio_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/checkout_gender_male" />
    <RadioButton
        android:id="@+id/radio_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/checkout_gender_female" />
</RadioGroup>

...            

<RadioGroup
    android:id="@+id/radio_payment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="4dp" >
</RadioGroup>

Code to add radio buttons. 用于添加单选按钮的代码。

RadioGroup paymentGroup = (RadioGroup) findViewById(R.id.radio_payment);
RadioGroup.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);        

for (String paymentType: checkoutData.getPaymentTypes()) {
    RadioButton radioButton = new RadioButton(getBaseContext());
    radioButton.setText(paymentType);
    paymentGroup.addView(radioButton, params);
}

How do I archive the same look and feel for the buttons in group 2 by code? 如何通过代码归档组2中按钮的相同外观?

UPDATE 1 更新1

I did some more testing. 我做了一些测试。

I've tested in the following configurations. 我已经在以下配置中进行了测试。

  • Emulator - Google Android 4.1.1: Same behavior 模拟器 - 谷歌Android 4.1.1:相同的行为
  • Emulator - Google Android 2.3.4: Same behavior but the graphics all RadioButtons are equal,but the text color still differs. 模拟器 - 谷歌Android 2.3.4:相同的行为,但所有RadioButtons的图形是相同的,但文本颜色仍然不同。 I guess that in this version of Android there is only one graphic for the button. 我想在这个版本的Android中只有一个按钮图形。
  • Device - Nexus One - Android Cyanogenmod 7 (Android 2.3.7): Same behavior as on the emulator with Android 2.3.4 设备 - Nexus One - Android Cyanogenmod 7(Android 2.3.7):与使用Android 2.3.4的模拟器上的行为相同

When I mix the second group up by adding one button in the layout file and two programmatically, the result is still the same. 当我通过在布局文件中添加一个按钮和以编程方式添加两个按钮来混合第二组时,结果仍然相同。 The first button (defined in the layout) looks like expected, the both other buttons use a different button graphic and have a different text color. 第一个按钮(在布局中定义)看起来像预期的,其他两个按钮使用不同的按钮图形并具有不同的文本颜色。

Ok I found the solution to my problem. 好的,我找到了解决问题的方法。

I used the wrong context to create the RadioButton. 我使用了错误的上下文来创建RadioButton。

Instead of 代替

RadioButton radioButton = new RadioButton(getBaseContext());

I have to use 我必须使用

RadioButton radioButton = new RadioButton(getContext);

or 要么

RadioButton radioButton = new RadioButton(this); // this is the Activity

I don't know why I used the base context here as I never used it before. 我不知道为什么我在这里使用了基本上下文,因为我之前从未使用它。 If I remember correctly then a Context object can contain information about the style an layout of an Activity. 如果我没记错,那么Context对象可以包含有关Activity的布局样式的信息。 I guess when I used the base context, this information was missing and therefore the radio buttons looked differently. 我想当我使用基本上下文时,这些信息丢失了,因此单选按钮的外观不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM