2012-08-08 29 views
1

我無法找到解決方案!我正在嘗試遵循Android 2.1及更高版本。AlertDialog與列表項和自定義主題

我嘗試創建一個相當於AlertDialog一個自定義主題。

我發現在API v.11之前無法在AlertDialog上應用主題。我嘗試使用ContextThemeWrapper,但找不到定製按鈕的解決方案。

對於一個簡單的觀點,我創造我自己的對話與我自己的內容視圖。我做了我想要的主題。

但是,當我想要一個AlertDialog使用自定義主題和列表項,它是比較複雜的。我找不到在列表末尾添加按鈕的解決方案。因爲當列表太大時,按鈕在窗口外面。

我試着用: - 一個RelativeLayout的: *下面標題 *按鈕標題 *的ListView下面的ListView - 一個的LinearLayout垂直

任何人有一個想法?

我添加了result needed

也許,我的最後一個,也是非常醜陋的想法是,用builder創建一個正常的AlertDialog,用findViewById查找每個視圖並應用所需的主題屬性......但是我必須看看自從Android以來,ID是否是常量2.1 ...

我的佈局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:bw="http://schemas.android.com/apk/res-auto/com.levelup.beautifulwidgets" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/ab_background" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:textColor="@color/grey_1" 
     android:textSize="24dp" /> 

    <FrameLayout 
     android:id="@+id/title_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="5dp" 
     android:paddingRight="10dp" /> 

    <ListView 
     android:id="@+id/container" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title" /> 

    <LinearLayout 
     android:id="@+id/buttons" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/container" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/cancel_button" 
      style="@style/DialogButton" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="-1dp" 
      android:layout_weight="1" 
      android:text="@string/cancel" /> 

     <Button 
      android:id="@+id/ok_button" 
      style="@style/DialogButton" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/ok" /> 
    </LinearLayout> 

</RelativeLayout> 
+0

這應該是可以用RelativeLayout實現的,你可以發佈你試過的代碼嗎?也許我們可以幫助你實現它。 – FoamyGuy 2012-08-08 16:06:57

+0

我只是添加了xml文件。 – JulienDR 2012-08-09 07:40:34

+0

我找到了一個解決方案。 我是一個垂直的LinearLayout。 所有的觀點都與layout_weight 0 只是ListView控件是1 – JulienDR 2012-08-09 13:48:59

回答

2

我找到了解決辦法。我是一個垂直的LinearLayout。所有的視圖都與一個layout_weight爲0.只是ListView是1.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/titleLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:padding="5dp" > 

     <FrameLayout 
      android:id="@+id/title_container" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:paddingRight="10dp" /> 

     <TextView 
      android:id="@+id/title" 
      style="@style/Dialog.Title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_toLeftOf="@id/title_container" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/containerLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" > 

     <FrameLayout 
      android:id="@+id/container" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:background="@drawable/dialog_header_divider" /> 
    </RelativeLayout> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_weight="0" 
     android:background="@color/grey_2" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/cancel_button" 
      style="@style/Dialog.Button.Cancel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="-1dp" 
      android:layout_weight="1" 
      android:text="@string/cancel" /> 

     <View 
      android:id="@+id/buttonSeparator" 
      android:layout_width="1dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="0" 
      android:background="@color/grey_2" /> 

     <Button 
      android:id="@+id/ok_button" 
      style="@style/Dialog.Button.Ok" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/ok" /> 
    </LinearLayout> 

</LinearLayout> 
+0

非常感謝你! – flobacca 2012-08-14 21:44:53