2012-02-06 21 views
0

我想在我的Android應用程序中顯示一個對話框讓用戶更改一些選項。如何佈置我的對話框=>滾動視圖=>可展開列表視圖?

我對佈局這個對話框的計劃是:

  • 對話框
    • 滾動型包裝的所有選項
      • 選項組中的1 - ExpandableListView
      • 選項組中的2 - 如以上
      • 頂級選項A - CheckedTextView
      • 頂級B選項 - CheckedTextView
      • 頂級備選方案C - CheckedTextView
    • 確定/取消按鈕

但是,當我試圖讓我最終佈局權要麼選項組合崩潰,以便ExpandableListView只有第一組的高度,或者ExpandableListView佔據整個對話框或類似的位置。

在下面的屏幕截圖中,您可以看到我的對話框,選項組爲藍色(僅顯示一個),頂級選項爲綠色,未使用的ScrollView空間爲紅色,OK /取消LinearLayout爲白色。

Screenshot of bad layout

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,則爲對話框的固定高度)。

有沒有簡單的方法來讓這個佈局正常工作?如果不是,我應該如何表示一個包含選項組和頂級選項的選項菜單?

回答

0

我已經看到了很多參數,將一個可滾動視圖放在另一個可滾動視圖內不起作用。我很確定這是不可能的。

0

你應該考慮重做你的應用程序的這部分,在對話中有太多的內容會讓用戶感到困惑。該對話框用於簡短而簡單的答案,而不是複雜的答案。

我的建議是改變你的設計方法。

+0

我可以在一個新的活動中放置相同的佈局,但是我只是在一個更大的窗口中出現相同的問題=> http://i.imgur.com/Sa8OG.jpg。 – George 2012-02-06 06:56:40

1

在新代碼中使用PreferenceActivity和PreferenceFragment。這樣你就不會重新發明輪子,你的應用將會適合平臺上的其他人。

+0

感謝您指出,我是Android新手,很高興瞭解它們。在這種情況下,該對話框用於改進搜索選項 - 例如用戶通過文本進行搜索,然後單擊「優化選項」按鈕。在一個類別中,他們可以勾選州(例如阿爾伯塔省,不列顛哥倫比亞省,馬尼托巴省)以將搜索限制在這些州,並且可以勾選其他複選框以類似地限制搜索結果。我懷疑這些類不適合非永久/非「設置」類型的東西 - 是嗎? – George 2012-02-06 22:48:48

相關問題