2010年9月19日日曜日

Android ScrollView + Dialog で横幅を window いっぱいにする

例えば、

 タイトル
 本文 (ScrollView 内に TextView)
 ボタン

というダイアログがあるとします。
Layout が

 LinearyLayout
  ScrollView
   TextView
  Button

になっているとします。
この場合、LinearyLayout, ScrollView, TextView で
layout_width="fill_parent" を指定すると
こんな残念な感じになります。



ちなみに xml は
  1. <!--xml version="1.0" encoding="utf-8"?-->  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:padding="10dip"  
  7.     android:gravity="center"  
  8.     android:background="#666666"  
  9.     >  
  10.     <ScrollView  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="fill_parent"  
  13.         android:layout_weight="1"  
  14.         >  
  15.         <TextView  
  16.             android:layout_width="fill_parent"  
  17.             android:layout_height="fill_parent"  
  18.             android:autoLink="web"  
  19.             android:text="Libraroid v5.11.235\n\n対応図書館..."  
  20.             />  
  21.     </ScrollView>  
  22.     <Button  
  23.         android:id="@+id/dialog_cancel_about"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="@string/dialog_return"  
  27.         android:layout_marginTop="10dip"  
  28.     />  
  29. </LinearLayout>  



ここで、ScrollView を layout_width="wrap_content"
にします。

するとなぜかダイアログが画面の横幅いっぱいになります。謎。



xml はこうなる
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:padding="10dip"  
  6.     android:gravity="center"  
  7.     android:background="#666666"  
  8.     >  
  9.     <ScrollView  
  10.         // fill_parent -> wrap_content  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="fill_parent"  
  13.         android:layout_weight="1"  
  14.      >  
  15.         ...  
  16.     </ScrollView>  
  17.     ...  
  18. </LinearLayout>  


こうすると、横幅いっぱいになってくれます。
layout_width="200dip" とか固定値にしても横幅は広くなりますが、
これのいいところは、画面の向きを横向きにした場合でもちゃんと
横長のダイアログになってくれるところです。
画面の解像度にもよらないので、マルチデバイスに対応するには
数字で指定するよりいいと思います。

画面いっぱいじゃなく、すこし隙間がほしいなと思って、
ルートの LinearLayout に layout_margin を設定したら
謎なことになりました。



グレーの部分が LinearLayout 部分。
なぜか右と下に padding っぽい隙間が!うーん謎。
ちなみに、xml はこんな感じ。

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:orientation="vertical"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:padding="10dip"  
  6.     android:gravity="center"  
  7.     android:background="#666666"  
  8.     // add layout_margin  
  9.     android:layout_margin="20dip"  
  10.     >  
  11.     ....  
  12. </LinearLayout>  

1 件のコメント:

  1. dialogの幅は謎ですよね。。
    自分は、高さ1pxの透明なTextViewを
    android:layout_width="fill_parent"
    でしこんで、幅を広げてます。

    返信削除