我正在嘗試創(chuàng)建一種比例視圖。因此,我將 RadioButtons 與 RadioButton 頂部的文本一起使用。在我的布局文件中定義它工作正常:<LinearLayout android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:layout_height="match_parent" android:layout_width="match_parent" android:text="Text on top" android:button="@null" android:drawableBottom="@android:drawable/btn_radio" android:gravity="center" android:layout_weight="1"/> <RadioButton android:layout_height="match_parent" android:layout_width="match_parent" android:text="Text on top" android:button="@null" android:drawableBottom="@android:drawable/btn_radio" android:gravity="center" android:layout_weight="1"/> </RadioGroup></LinearLayout>結(jié)果如下所示:現(xiàn)在我只需要RadioGroup。我想以編程方式創(chuàng)建RadioButtons 并將它們添加到 RadioGroup。我試過這段代碼,但它不起作用:RadioButton rb = new RadioButton(Context);LinearLayout.LayoutParams rbParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);rb.SetButtonDrawable(null);rb.SetCompoundDrawablesWithIntrinsicBounds(null,null,null, Resources.GetDrawable(Android.Resource.Drawable.ButtonRadio));rb.Text = "Test";rb.Gravity = GravityFlags.Center;rbParams.Weight = 1;rb.LayoutParameters = rbParams;radioGroup.AddView(rb);我認(rèn)為SetButtonDrawable和/或SetCompoundDrawable與android:button和不同android:drawableBottom。
2 回答

牧羊人nacy
TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊
我認(rèn)為您忘記將布局參數(shù)設(shè)置為您的單選按鈕。只需添加rb.setLayoutParams(rbParams);
添加您之前rb
給你的radioGroup
- 2 回答
- 0 關(guān)注
- 219 瀏覽
添加回答
舉報(bào)
0/150
提交
取消