Java RadioButton未显示
我试图在XML布局中实现一个RadioGroup项。但是,编译的代码很好,在应用程序中,没有显示RadioGroup(XML文件的最后四分之一)。我读到一个原因可能是线性布局的方向,但在我的情况下,这看起来很适合我。正如您在我的代码中所看到的,我已经实现了一些CardView(它们都工作得很好),这让我产生了这样的想法,即RadioGroup本身有问题。我还将发布部分Java代码,因为这也可能会影响RadioGroup的大小。 谢谢你的帮助 XML布局Java RadioButton未显示,java,android,android-layout,radio-group,Java,Android,Android Layout,Radio Group,我试图在XML布局中实现一个RadioGroup项。但是,编译的代码很好,在应用程序中,没有显示RadioGroup(XML文件的最后四分之一)。我读到一个原因可能是线性布局的方向,但在我的情况下,这看起来很适合我。正如您在我的代码中所看到的,我已经实现了一些CardView(它们都工作得很好),这让我产生了这样的想法,即RadioGroup本身有问题。我还将发布部分Java代码,因为这也可能会影响RadioGroup的大小。 谢谢你的帮助 XML布局 <?xml version="1.
<?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>
我认为您应该将单选按钮放在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>
我已经检查了您的代码。就我而言,我看到了单选按钮。你的实际问题是什么?你能用截图解释一下吗possible@Md.Asaduzzaman谢谢你抽出时间。我在帖子中添加了一个截图。我正在一台真正的设备上运行它……可能是API有问题吗?您是否正确配置了ButterKnife
?或者您的普通食品。选择的食品有一些项目吗?请调试并检查ButterKnif是否设置正确。我的Common.selectedFood还有在另一个类中定义的项目。
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();
<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>