我正在嘗試創建一種比例視圖。因此,我將 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>結果如下所示:現在我只需要RadioGroup。我想以編程方式創建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);我認為SetButtonDrawable和/或SetCompoundDrawable與android:button和不同android:drawableBottom。
2 回答

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
我認為您忘記將布局參數設置為您的單選按鈕。只需添加rb.setLayoutParams(rbParams);
添加您之前rb
給你的radioGroup
- 2 回答
- 0 關注
- 230 瀏覽
添加回答
舉報
0/150
提交
取消