【发布时间】:2022-01-03 13:26:14
【问题描述】:
TL;DR
如何制作分段的 RadioGroup 并将其设置为 AlertDialog.SetSingleChoiceItems 的样式?更准确地说:如何以与 AlertDialog.SetSingleChoiceItems 中的内容缩进相同的方式缩进 RadioGroup 中的选项?
上下文
用户正在获得一个警报对话框,他们需要在其中通过 RadioButtons 做出选择。有两种情况,我需要类似的风格:
- 选项显示在常规 AlertDialog.SetSingleChoiceItems 中
乙:Some options are recommended
- 推荐的选项显示在顶部。不推荐的选项显示在一行下方
代码
private void ShowAlert(object sender, EventArgs eventArgs)
{
var dialogBuilder = new Android.App.AlertDialog.Builder(this);
dialogBuilder.SetTitle("My Title");
string[] recommendedItems = { "Radio button 1", "Radio button 2"};
string[] unrecommendedItems = { "Radio button 3", "Radio button 4" };
List<RadioButtonItem> items = new List<RadioButtonItem>() {
new RadioButtonItem { Label = "Radio button 1", Recommended = true},
new RadioButtonItem { Label = "Radio button 2", Recommended = false},
new RadioButtonItem { Label = "Radio button 3", Recommended = false},
new RadioButtonItem { Label = "Radio button 4", Recommended = true},
};
RadioGroup radioGroup = new RadioGroup(this);
TextView recommendedText = new TextView(this)
{
Text = "Recommended"
};
radioGroup.AddView(recommendedText);
addRadioButtons(radioGroup, items, true);
//Add divider between the recommended/unrecommended options
LinearLayout divider = new LinearLayout(this);
var dividerSize = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 1);
divider.LayoutParameters = dividerSize;
divider.SetBackgroundColor(new Color(Color.DimGray));
radioGroup.AddView(divider);
addRadioButtons(radioGroup, items, false);
dialogBuilder.SetView(radioGroup);
dialogBuilder.SetPositiveButton("OK", delegate { });
dialogBuilder.Show();
}
private void addRadioButtons(RadioGroup radioGroup, List<RadioButtonItem> items, bool recommended)
{
for (int i = 0; i < items.Count; i++)
{
RadioButtonItem item = items[i];
RadioButton rb = new RadioButton(this) { Text = item.Label };
if (item.Recommended == recommended)
{
radioGroup.AddView(rb);
}
}
}
问题
当我尝试像这样首先设置第二个场景的样式时,问题就出现了。我无法缩进选项,而不会弄得一团糟。
- 如果我缩进整个 radioGroup,那么分隔线也会得到 indented。
- 如果我为单选按钮添加填充,那么the text moves, but the circles stay
- 我无法将按钮包装在可以添加填充的东西中,因为按钮需要是 RadioGroup 的直接子级才能起作用
【问题讨论】:
标签: android xamarin xamarin.android radio-button radio-group