java - 单选按钮不显示

标签 java android android-layout android-radiogroup android-radiobutton

我的应用程序中有 2 个单选按钮。它们都在 AndroidStudio 中以 xml 显示,但是当我在手机上运行应用程序时,它们消失了。这不是因为它们的定位或一些随机功能 - 为了测试,我放置了另一个 RadioButton 并且没有对它做任何事情。当我运行该应用程序时,它也没有显示。我看到的唯一问题是样式方面的问题,但它们似乎还不错。我仍然会发布代码:

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Male"
    android:id="@+id/maleBtn" />

<RadioButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Female"
    android:id="@+id/femaleBtn"
    android:layout_toRightOf="@+id/maleBtn"
    android:layout_marginLeft="10dp" />

样式.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.ActionBar">
        <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="My.Button" parent="Widget.AppCompat.Button">
        <item name="android:textAllCaps">false</item>
    </style>
</resources>

最佳答案

试试这个代码,我相信你的 RadioButton 的布局父级正在改变可见性或者由于父级的方向你无法看到 RadioButton

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:orientation="horizontal">


        <RadioButton
            android:id="@+id/radioButton_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.5"
            android:text="Male" />


        <RadioButton
            android:id="@+id/radioButton_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="0.5"
            android:text="Female" />
    </RadioGroup>

关于java - 单选按钮不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34619337/

相关文章:

java - ScrollView隐藏标题栏

android - 后退按钮未显示在 CollapsingToolbarLayout 中

java - Immutable.listCopyfOf 抛出 ArrayIndexOutOfBoundsException

java - JTable 不显示输出

java - 如何将 id 传递到 Parcelable 中?

android - 如何将 Activity 转换为 fragment 以在抽屉中使用

java - aspose 单词中的特殊字符编码

java - 使用java代码登录后向网站服务器发送响应代码500

android - 如何让 TabLayout 占满屏幕宽度?

java - 如何改变图片按钮的背景资源来实现开关机