我想在我的Android應用程序中顯示一個對話框讓用戶更改一些選項。如何佈置我的對話框=>滾動視圖=>可展開列表視圖?
我對佈局這個對話框的計劃是:
- 對話框
- 滾動型包裝的所有選項
- 選項組中的1 - ExpandableListView
- 選項組中的2 - 如以上
- 頂級選項A - CheckedTextView
- 頂級B選項 - CheckedTextView
- 頂級備選方案C - CheckedTextView
- 確定/取消按鈕
- 滾動型包裝的所有選項
但是,當我試圖讓我最終佈局權要麼選項組合崩潰,以便ExpandableListView只有第一組的高度,或者ExpandableListView佔據整個對話框或類似的位置。
在下面的屏幕截圖中,您可以看到我的對話框,選項組爲藍色(僅顯示一個),頂級選項爲綠色,未使用的ScrollView空間爲紅色,OK /取消LinearLayout爲白色。
refine_dialog.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">
<!-- ScrollView so that the user can see all the options
Without filling the entire dialog and losing the ok/cancel buttons
The height is hardcoded - I'd like to make it fill_parent -->
<ScrollView
android:background="#FFFF0000"
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="300dp"> <!--RED-->
<!-- Linear layout around the options
ScrollView complains if it wraps multiple children -->
<LinearLayout
android:background="#FF00FF00"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--GREEN-->
<!-- Option groups -->
<ExpandableListView
android:id="@+id/elvRefine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000FF" /> <!--BLUE-->
<!-- Top level options -->
<CheckedTextView
android:id="@+id/checkboxA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top level option A" />
<CheckedTextView
android:id="@+id/checkboxB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top level option B" />
<CheckedTextView
android:id="@+id/checkboxC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top level option C" />
</LinearLayout>
</ScrollView>
<!-- OK, cancel buttons -->
<LinearLayout
android:background="#FFFFFFFF"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"> <!--WHITE-->
<Button
android:id="@+id/buttonOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/buttonCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
getRefineDialog()
private Dialog getRefineDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.refine_dialog);
dialog.setTitle("Refine options");
dialog.setCancelable(true);
final ExpandableListView elv =
(ExpandableListView) dialog.findViewById(R.id.elvRefine);
elv.setAdapter(new RefineAdapter(this));
elv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// cancel button listener
Common.setSimpleCancelListener(dialog, R.id.buttonCancel);
return dialog;
}
的ExpandableListView documentation說:
不能使用值包如果父級的大小也沒有嚴格指定,則爲XML中的ExpandableListView的android:layout_height屬性的_content(例如,如果父級是ScrollView,則無法指定wrap_content,因爲它也可以是任意長度。但是,如果ExpandableListView父級具有特定的大小(如100像素),則可以使用wrap_content。
......但我不認爲這應該是一個問題,因爲ScrollView的固定高度爲300dp(或者如果父對話框設置爲match_parent,則爲對話框的固定高度)。
有沒有簡單的方法來讓這個佈局正常工作?如果不是,我應該如何表示一個包含選項組和頂級選項的選項菜單?
我可以在一個新的活動中放置相同的佈局,但是我只是在一個更大的窗口中出現相同的問題=> http://i.imgur.com/Sa8OG.jpg。 – George 2012-02-06 06:56:40