簡體   English   中英

RadioButton 沒有顯示

[英]RadioButton is not showing

我試圖在我的 XML 布局中實現一個 RadioGroup 項目。 代碼編譯得很好,但是,在應用程序中,RadioGroup(XML 文件的最后四分之一)沒有顯示。 我讀到一個原因可能是 Linearlayout 的方向,但就我而言,這看起來很適合我。 正如您在我的代碼中看到的那樣,我已經實現了一些 CardView(它們都工作得很好),這讓我想到 RadioGroup 本身有問題。 我還將發布我的部分 Java 代碼,因為它也可能會影響 RadioGroup 的大小。 謝謝你的幫助。

不存在的 RadioButton 的圖像

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 和 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();


第二個<LinearLayout android:layout_width="match_parent" />是無意義的。

最好將android:layout_width="0dp"android:layout_weight="1.00"

...以免將尾隨的一個節點推到屏幕邊界之外。

我認為您應該將 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