简体   繁体   English

RadioButton 没有显示

[英]RadioButton is not showing

I tried to implement a RadioGroup item to my XML layout.我试图在我的 XML 布局中实现一个 RadioGroup 项目。 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-Fragment 和 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();


The second one <LinearLayout android:layout_width="match_parent" /> is non-sense. 第二个<LinearLayout android:layout_width="match_parent" />是无意义的。

Better set android:layout_width="0dp" together with android:layout_weight="1.00" 最好将android:layout_width="0dp"android:layout_weight="1.00"

... in order not to have the trailing one node pushed outside of the screen boundaries. ...以免将尾随的一个节点推到屏幕边界之外。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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