简体   繁体   中英

Android RadioButton shows only text, not the actual button in device emulator

I have a group of radio buttons displaying fine in my XML design window, but in the emulator (and on my actual device that I installed the app on) it only shows the button text and not the button.

截图

Here is the XML file for this activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Question1">

<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="25dp"
    android:layout_marginLeft="25dp"
    android:layout_marginEnd="25dp"
    android:layout_marginRight="25dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <TextView
        android:id="@+id/errorView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="@string/error_msg"
        android:textColor="@android:color/holo_red_light"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="@string/my_gender_is"
        android:textSize="20sp"
        android:textStyle="bold" />

    <RadioGroup
        android:id="@+id/gender_group"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <RadioButton
            android:id="@+id/radio_male"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="@string/male"
            android:textColor="#636363"
            android:textSize="18sp" />

        <RadioButton
            android:id="@+id/radio_female"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/female"
            android:textColor="#636363"
            android:textSize="18sp" />

    </RadioGroup>

</LinearLayout>

<Button
    android:id="@+id/nextPage"
    android:layout_width="120dp"
    android:layout_height="50dp"
    android:layout_marginStart="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginBottom="15dp"
    android:layout_weight="1"
    android:background="@color/colorPrimary"
    android:fontFamily="@font/segoeuil"
    android:gravity="center"
    android:includeFontPadding="false"
    android:paddingTop="-9dp"
    android:paddingBottom="5dp"
    android:text="@string/next"
    android:textAlignment="gravity"
    android:textAllCaps="false"
    android:textSize="26sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.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>
        <item name="android:colorBackground">@color/background</item>

    </style>

    <!-- No title bar theme -->
    <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:colorBackground">@color/background</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:textColorPrimaryInverse">@color/primary_text_material_dark</item>
    </style>

</resources>

It looks fine in the preview but on my device and in the emulator is seems to exclude the radio buttons.

You are using in a wrong way I guess use it as following:

First declare your custom styles in styles.xml file like this:

    <style name="MyRaidoButton" parent="Widget.AppCompat.CompoundButton.RadioButton">
    <item name="colorAccent">#0091ea</item>
    <item name="android:textColorPrimaryDisableOnly">#f44336</item>
    <item name="android:textAppearance">@style/TextAppearance.AppCompat</item>
</style>

Once the style is done now all you need is to assign that particular style to your radio button or group like the following:

<RadioButton
android:id="@+id/products"
. . .
android:theme="@style/MyRaidoButton"/>

For more info take a look at the following links:

http://www.zoftino.com/android-ui-control-radiobutton

How to customize default theme of radio button in android?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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