簡體   English   中英

無法在Android應用中設置復選框和單選按鈕的樣式

[英]Not able styling checkboxes and radio buttons in android app

我無法在styles.xml中為Android應用程序中的復選框和單選按鈕制作工作樣式。 僅當我將復選框和單選按鈕的屬性直接添加到activity_main.xml文件中時,它們才起作用,而如果我將它們定義為樣式,則它們不起作用。 您可以幫我解決此問題嗎? 非常感謝

在這里,您正在談論的文件是:

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="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="com.marconota.soccerquiz.MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="1"
        android:scaleType="centerCrop"
        android:src="@drawable/soccer_field" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">

        <LinearLayout
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingBottom="@dimen/padding"
            android:paddingLeft="@dimen/padding"
            android:paddingRight="@dimen/padding"
            android:paddingTop="@dimen/padding">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/paddingbottomsubtitle"
                android:text="@string/test_your_soccer_knowledge"
                android:textAppearance="@style/subtitle" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/which_team_did_win_2014_world_cup"
                android:textAppearance="@style/questions" />

            <!-- START*** CardView Container *** -->
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardBackgroundColor="#80d1d1d1"
                card_view:cardCornerRadius="@dimen/cardcornerradius"
                card_view:cardElevation="@dimen/cardelevation"
                card_view:cardPreventCornerOverlap="true"
                card_view:cardUseCompatPadding="true"
                card_view:contentPadding="@dimen/cardcontentpadding">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingRight="@dimen/padding">

                    <RadioGroup
                        android:id="@+id/radiogroup_question1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <RadioButton
                            android:id="@+id/italy"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/italy_item_question1"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/brazil"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/brazil_question1"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/germany"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="8dp"
                            android:buttonTint="@android:color/white"
                            android:text="@string/germany"
                            android:textColor="@android:color/white" />
                    </RadioGroup>

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:src="@drawable/brazil2014_def" />

                </RelativeLayout>

            </android.support.v7.widget.CardView>
            <!-- END*** CardView Container *** -->

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/who_did_win_more_golden_ball_awards"
                android:textAppearance="@style/questions" />

            <!-- START*** CardView Container *** -->
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardBackgroundColor="#80d1d1d1"
                card_view:cardCornerRadius="@dimen/cardcornerradius"
                card_view:cardElevation="@dimen/cardelevation"
                card_view:cardPreventCornerOverlap="true"
                card_view:cardUseCompatPadding="true"
                card_view:contentPadding="@dimen/cardcontentpadding">

                <EditText
                    android:id="@+id/question2_edittext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:hint="@string/please_insert_player_surname_here"
                    android:textAppearance="@style/edit_text"
                     />

            </android.support.v7.widget.CardView>
            <!-- END*** CardView Container *** -->

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/who_did_score_more_goals_in_mexico_86"
                android:textAppearance="@style/questions" />

            <!-- START*** CardView Container *** -->
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardBackgroundColor="#80d1d1d1"
                card_view:cardCornerRadius="@dimen/cardcornerradius"
                card_view:cardElevation="@dimen/cardelevation"
                card_view:cardPreventCornerOverlap="true"
                card_view:cardUseCompatPadding="true"
                card_view:contentPadding="@dimen/cardcontentpadding">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingRight="@dimen/padding">

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <RadioButton
                            android:id="@+id/lineker"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/lineker"
                            android:textColor="@android:color/white"/>

                        <RadioButton
                            android:id="@+id/rivera"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/rivera"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/voeller"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="@dimen/margin"
                            android:buttonTint="@android:color/white"
                            android:text="@string/voeller"
                            android:textColor="@android:color/white" />
                    </RadioGroup>

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:src="@drawable/mexico86logo_def" />
                </RelativeLayout>


            </android.support.v7.widget.CardView>
            <!-- END*** CardView Container *** -->


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/margin"
                android:text="@string/which_team_did_win_more_world_cups"
                android:textAppearance="@style/questions" />


            <!-- START*** CardView Container *** -->
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardBackgroundColor="#80d1d1d1"
                card_view:cardCornerRadius="@dimen/cardcornerradius"
                card_view:cardElevation="@dimen/cardelevation"
                card_view:cardPreventCornerOverlap="true"
                card_view:cardUseCompatPadding="true"
                card_view:contentPadding="@dimen/cardcontentpadding">


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingRight="@dimen/padding">

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <RadioButton
                            android:id="@+id/italy_question4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/italy_question4"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/brazil_question4"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/brazil_question4"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/holland"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="@dimen/margin"
                            android:buttonTint="@android:color/white"
                            android:text="@string/holland"
                            android:textColor="@android:color/white" />
                    </RadioGroup>

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:src="@drawable/wold_cup_def" />
                </RelativeLayout>

            </android.support.v7.widget.CardView>
            <!-- END*** CardView Container *** -->

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/margin"
                android:text="@string/who_did_belong_italian_team_in_italy_90"
                android:textAppearance="@style/questions" />

            <!-- START*** CardView Container *** -->
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardBackgroundColor="#80d1d1d1"
                card_view:cardCornerRadius="@dimen/cardcontentpadding"
                card_view:cardElevation="@dimen/cardelevation"
                card_view:cardPreventCornerOverlap="true"
                card_view:cardUseCompatPadding="true"
                card_view:contentPadding="@dimen/cardcontentpadding">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingRight="@dimen/padding">

                    <CheckBox
                        android:id="@+id/schillaci"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:buttonTint="@android:color/white"
                        android:text="@string/schillaci"
                        android:textColor="@android:color/white" />

                    <CheckBox
                        android:id="@+id/casiraghi"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/schillaci"
                        android:buttonTint="@android:color/white"
                        android:text="@string/casiraghi"
                        android:textColor="@android:color/white" />

                    <CheckBox
                        android:id="@+id/dossena"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/casiraghi"
                        android:layout_marginBottom="@dimen/margin"
                        android:buttonTint="@android:color/white"
                        android:text="@string/dossena"
                        android:textColor="@android:color/white" />

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:src="@drawable/italy90_italy_team_def" />
                </RelativeLayout>


            </android.support.v7.widget.CardView>
            <!-- END*** CardView Container *** -->

            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/who_did_win_his_fourth_golden_ball_in_2016"
                android:textAppearance="@style/questions" />


            <!-- START*** CardView Container *** -->
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                card_view:cardBackgroundColor="#80d1d1d1"
                card_view:cardCornerRadius="@dimen/cardcornerradius"
                card_view:cardElevation="@dimen/cardelevation"
                card_view:cardPreventCornerOverlap="true"
                card_view:cardUseCompatPadding="true"
                card_view:contentPadding="@dimen/cardcontentpadding">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingRight="@dimen/padding">

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <RadioButton
                            android:id="@+id/cristiano_ronaldo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/cristiano_ronaldo"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/benzema"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/benzema"
                            android:textColor="@android:color/white" />

                        <RadioButton
                            android:id="@+id/ibrahimovic"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:buttonTint="@android:color/white"
                            android:text="@string/ibrahimovic"
                            android:textColor="@android:color/white" />
                    </RadioGroup>

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:src="@drawable/golden_ball_def" />
                </RelativeLayout>


            </android.support.v7.widget.CardView>
            <!-- END*** CardView Container *** -->


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />

                <android.support.design.widget.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:layout_marginTop="16dp"
                    android:onClick="displayScore"
                    android:src="@drawable/ok"
                    app:borderWidth="0dp"
                    app:elevation="6dp"
                    app:pressedTranslationZ="12dp" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

styles.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>
        <item name="android:textColorHint">@color/textColorHint</item>
        <item name="android:textColorPrimary">@color/textColorPrimary</item>
    </style>
    <!-- Style for questions-->
    <style name="questions">
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/textColorQuestions</item>
        <item name="android:textSize">@dimen/textsizequestions</item>
        <item name="android:textAllCaps">false</item>
    </style>
    <!-- Style for subtitle -->
    <style name="subtitle">
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/textColorSubtitle</item>
        <item name="android:textSize">@dimen/textsizesubtitle</item>
        <item name="android:textAllCaps">false</item>
    </style>
    <!-- Style for edit text -->
    <style name="edit_text">
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/textColorEditText</item>
        <item name="android:textSize">@dimen/textsizeedittext</item>
        <item name="android:textAllCaps">false</item>
    </style>
    <!-- Style for items -->
    <style name="items">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textAllCaps">false</item>
    </style>



</resources>

在要包含樣式屬性的視圖中,使用:

style="@style/your_style"

這可以給你一個主意:

<style name="myTheme" parent="@android:Theme.Holo">
    <!-- override default check box style with custom checkBox -->
    <item name="android:checkboxStyle">@style/CustomCheckBox</item>
    <item name="android:radioButtonStyle">@style/CustomRadioButton</item>
</style>

<style name="CustomCheckBox" 
   parent="@android:style/Widget.CompoundButton.CheckBox">
    <item name="android:button">@drawable/foo</item>
    <item name="android:textColor">@android:color/holo_purple</item>
</style>

<!-- custom style -->
<style name="CustomRadioButton"
     parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:button">@drawable/foo</item>
</style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM