Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中以编程方式将单选按钮添加到RadioGroup时,其样式会有所不同_Android_Styles_Radio Button_Radio Group - Fatal编程技术网

在Android中以编程方式将单选按钮添加到RadioGroup时,其样式会有所不同

在Android中以编程方式将单选按钮添加到RadioGroup时,其样式会有所不同,android,styles,radio-button,radio-group,Android,Styles,Radio Button,Radio Group,我正在Android应用程序中构建表单 表单有多个字段,其中两个组件是放射组。包含其按钮的第一个组在活动的布局文件中完全定义。对于第二个组,仅在布局文件中定义了RadioGroup元素,其中在运行时将单选按钮添加到组中 如下图所示,我遇到了一些造型问题。第二组中的单选按钮与第一组中的按钮看起来不同。第二组的按钮图像和文本颜色不同。除了按钮的方向之外,两个放射组都配置了相同的属性。当我直接在布局文件中添加第二组的按钮时,它们看起来与第一组相同 布局文件 <RadioGroup an

我正在Android应用程序中构建表单

表单有多个字段,其中两个组件是放射组。包含其按钮的第一个组在活动的布局文件中完全定义。对于第二个组,仅在布局文件中定义了RadioGroup元素,其中在运行时将单选按钮添加到组中

如下图所示,我遇到了一些造型问题。第二组中的单选按钮与第一组中的按钮看起来不同。第二组的按钮图像和文本颜色不同。除了按钮的方向之外,两个放射组都配置了相同的属性。当我直接在布局文件中添加第二组的按钮时,它们看起来与第一组相同

布局文件

<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>
如何按代码归档组2中按钮的相同外观和感觉

更新1

我做了更多的测试

我已经在以下配置中进行了测试

  • 仿真器-谷歌安卓4.1.1:相同的行为
  • Emulator-GoogleAndroid2.3.4:行为相同,但所有单选按钮的图形都相同,但文本颜色仍然不同。我猜在这个版本的Android中,按钮只有一个图形
  • 设备-NexusOne-Android Cyanogenmod 7(Android 2.3.7):与使用Android 2.3.4的模拟器上的行为相同

当我通过在布局文件中添加一个按钮和两个编程按钮来混合第二个组时,结果仍然是一样的。第一个按钮(在布局中定义)看起来与预期的一样,其他两个按钮使用不同的按钮图形并具有不同的文本颜色

好的,我找到了解决问题的办法

我使用了错误的上下文来创建单选按钮

而不是

RadioButton radioButton = new RadioButton(getBaseContext());
我必须使用

RadioButton radioButton = new RadioButton(getContext);


我不知道为什么在这里使用基本上下文,因为我以前从未使用过它。如果我没记错的话,那么上下文对象可以包含关于活动的样式和布局的信息。我猜当我使用基本上下文时,这些信息丢失了,因此单选按钮看起来不一样

只是一个早期的想法。创建一个扩展RadioButton的虚拟MyRadioButton,并设置运行代码的设备,怎么样。在模拟器上运行它是否也有同样的问题?什么API?因为我的结果没有什么不同。是同一视图父视图中的射线组。如果viewparent没有附加特定的样式?我目前正在使用Nexus S和Android 4.1.1进行开发。但我会在模拟器和其他设备上检查它,然后更新我的问题。将@+id/radio_gender“放在与@+id/radio_payment相同的视图组中,看看它们是否相同。如果是,则radio_性别具有从父对象附加的样式。两个radio组具有与父对象相同的视图组。
RadioButton radioButton = new RadioButton(getContext);
RadioButton radioButton = new RadioButton(this); // this is the Activity