TL;博士
如何制作分段的 RadioGroup 并將其設定為 AlertDialog.SetSingleChoiceItems 的樣式?更準確地說:如何以與 AlertDialog.SetSingleChoiceItems 中的內容縮進相同的方式縮進 RadioGroup 中的選項?
語境
用戶正在獲得一個警報對話框,他們需要在其中通過 RadioButtons 做出選擇。有兩種情況,我需要類似的風格:
A:不推薦任何選項
- 選項顯示在常規 AlertDialog.SetSingleChoiceItems
B:推薦一些選項
- 推薦的選項顯示在頂部。不推薦的選項顯示在一行下方
代碼
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,那么分隔線也會縮進。
- 如果我向單選按鈕添加填充,則文本會移動,但圓圈會保留
- 我無法將按鈕包裝在可以添加填充的東西中,因為按鈕需要是 RadioGroup 的直接子級才能起作用
uj5u.com熱心網友回復:
我發現radiobutton的margin_start屬性可以控制按鈕和組的距離。但是我們不能只在xml中的代碼中設定它。所以我使用了另一種方式,它使用線性布局來包含兩個無線電組和除法器。代碼如下:
線性輸出.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
方法代碼:
private void ShowAlert()
{
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},
};
LayoutInflater layoutInflater = LayoutInflater.From(this);
LinearLayout linearLayout = (LinearLayout)layoutInflater.Inflate(Resource.Layout.item2, null);
RadioGroup radioGroup1 = new RadioGroup(this);
radioGroup1.SetPadding(100, 0, 0, 0);
RadioGroup radioGroup2 = new RadioGroup(this);
radioGroup2.SetPadding(100, 0, 0, 0);
TextView recommendedText = new TextView(this)
{
Text = "Recommended"
};
linearLayout.AddView(recommendedText);
addRadioButtons(radioGroup1, items, true);
linearLayout.AddView(radioGroup1);
LinearLayout divider = new LinearLayout(this);
var dividerSize = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, 1);
divider.LayoutParameters = dividerSize;
divider.SetBackgroundColor(new Color(Color.DimGray));
linearLayout.AddView(divider);
addRadioButtons(radioGroup2, items, false);
linearLayout.AddView(radioGroup2);
dialogBuilder.SetView(linearLayout);
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);
}
}
}
uj5u.com熱心網友回復:
我最終通過將 radioGroup 的 clipToPadding 設定為 false 解決了這個問題。然后我將縮進的距離作為填充添加到 radioGroup
RadioGroup radioGroup = new RadioGroup(this);
int indentedDistance = 60;
radioGroup.SetClipToPadding(false);
radioGroup.SetPadding(indentedDistance, 0, 0, 0);
通過將其添加為負值,從分隔線中移除的相同距離
var dividerStyle = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 1);
dividerStyle.SetMargins(-indentedDistance, 0, 0, 0);
divider.LayoutParameters = dividerStyle;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/406449.html
標籤:
