Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - Android单选按钮未显示(Android Radio Button is not showing)

I tried to implement a RadioGroup item to my XML layout.

(我试图将RadioGroup项目实现为XML布局。)

The code compiled just fine, however, in the App the RadioGroup (last quarter of the XML-file) is not showing.

(编译好的代码很好,但是,在RadioGroup中(XML文件的最后四分之一)没有显示出来。)

I read that a reason could be the orientation of the Linearlayout, but in my case, that looks right for me.

(我读到一个原因可能是Linearlayout的方向,但就我而言,这很适合我。)

As you can see in my code I already implemented some CardViews (they all work perfectly) what leads me to the idea that something with the RadioGroup itself is wrong.

(正如您在我的代码中看到的那样,我已经实现了一些CardView(它们都可以正常工作),这使我想到RadioGroup本身存在问题。)

I will also post parts of my Java code because it's also likely to affect the size of the RadioGroup.

(我还将发布Java代码的一部分,因为它也可能会影响RadioGroup的大小。)

Thanks for your help.

(谢谢你的帮助。)

不存在的RadioButton的图像

XML-Layout

(XML布局)

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/colorBlack">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/food_name"
                        android:layout_marginTop="8dp"
                        android:padding="12dp"
                        android:text="Food Name"
                        android:textSize="18sp"
                        android:textColor="@color/colorAccent"
                        android:textStyle="bold"/>


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/layout_price"
                        android:orientation="horizontal">

                        <ImageView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:src="@drawable/ic_attach_money_black_24dp"/>

                        <TextView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:id="@+id/food_price"
                            android:layout_weight="9"
                            android:text="1000"
                            android:textColor="@color/colorAccent"
                            android:textSize="16sp"
                            android:textStyle="bold"/>

                    </LinearLayout>
                    <com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
                        android:id="@+id/number_button"
                        android:layout_width="80dp"
                        android:layout_height="25dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginBottom="18dp"
                        app:backGroundColor="@color/colorAccent"
                        app:finalNumber="20"
                        app:initialNumber="1"
                        app:textSize="6sp" />





                </LinearLayout>



            </androidx.cardview.widget.CardView>


            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true">

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

                    <RatingBar
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/ratingBar"
                        style="?android:attr/ratingBarStyleIndicator"
                        android:numStars="5"
                        android:stepSize="0.5"
                        android:isIndicator="true"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/food_description"
                        android:layout_marginTop="12dp"
                        android:lineSpacingMultiplier="1.5"
                        android:padding="12dp"
                        android:text="Description"
                        android:textColor="@android:color/black"
                        android:textSize="14sp"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>


            <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="8dp"
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="8dp">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/size"
                        android:textSize="22sp"/>

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="3"
                        android:id="@+id/rdi_group_size"/>

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/btnShowComment"
                android:layout_alignParentBottom="true"
                android:layout_margin="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:text="Show Comment"
                android:background="@drawable/roundbtn"
                android:textColor="@color/colorWhite"
                app:cornerRadius="4dp"/>

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <com.andremion.counterfab.CounterFab
        android:id="@+id/btnCart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_shopping_cart_black_24dp"
        app:backgroundTint="@android:color/white"
        app:elevation="6dp"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|end"
        app:pressedTranslationZ="12dp"
        app:useCompatPadding="true"/>

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing"
            android:layout_height="350dp"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="#0e0d0e"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/img_food"
                android:contentDescription="@null"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/btn_rating"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:backgroundTint="@android:color/white"
        android:src="@drawable/ic_star_black_24dp"
        app:elevation="6dp"
        app:layout_anchor="@id/app_bar_layout"
        app:layout_anchorGravity="bottom|right|start"
        app:pressedTranslationZ="12dp"
        app:useCompatPadding="true"/>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

Java-Fragment and BindView

(Java片段和BindView)

@BindView(R.id.rdi_group_size)
    RadioGroup rdi_group_size;
 for(SizeModel sizeModel: Common.selectedFood.getSize())
        {
            RadioButton radioButton=new RadioButton(getContext());
            radioButton.setOnCheckedChangeListener((compoundButton, b) -> {
                if(b)
                    Common.selectedFood.setUserSelectedSize(sizeModel);
                calculateTotalPrice();

            });
            LinearLayout.LayoutParams params =new LinearLayout.LayoutParams(0,
                    LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
            radioButton.setLayoutParams(params);
            radioButton.setText(sizeModel.getName());
            radioButton.setTag(sizeModel.getPrice());

            rdi_group_size.addView(radioButton);

        }

        if(rdi_group_size.getChildCount()>0)
        {
            RadioButton radioButton =(RadioButton)rdi_group_size.getChildAt(0);
            radioButton.setChecked(true);
        }
        calculateTotalPrice();


<span class="fc-light mr2" style=

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you should put the RadioButtons in RadioGroup tag, for example:

(我认为您应该将RadioButtons放在RadioGroup标记中,例如:)

<RadioGroup
    android:id="@+id/myRadioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checkedButton="@+id/btn1" >

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

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

    <RadioButton
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3" />
</RadioGroup>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...