ラベル tips の投稿を表示しています。 すべての投稿を表示
ラベル tips の投稿を表示しています。 すべての投稿を表示

2012年1月31日火曜日

android.app.Dialogを任意のサイズに設定する。 FILL_PARENT, WRAP_CONTENTもできるよ

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
さんざんハマったので。

こうだ


こうです。
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

android.app.DialogをnewしてレイアウトぶちこんだあとgetWindow()してsetLayoutでサイズ指定する感じです。
この時FILL_PARENT, WRAP_CONTENTなども使えます。

import android.widget.LinearLayout.LayoutParams;

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.hoge);
dialog.setTitle(R.string.moge);
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);




Dialogの横だけFILL_PARENTにする


まぁこうですね。

import android.widget.LinearLayout.LayoutParams;

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.hoge);
dialog.setTitle(R.string.moge);
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);




Dialogを300x300にしてみる


まぁこうですね。

import android.widget.LinearLayout.LayoutParams;

Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.hoge);
dialog.setTitle(R.string.moge);
dialog.getWindow().setLayout(300, 300);




使ったレイアウトXML


は、これ。android:textにリテラルぶっこむとLint先生に怒られるので良い子の皆様は必ずvaluesで定義してstringを下さい。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="(さよならだ)"
        android:textColor="#FFF" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button 
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="OK"
        />    
    </LinearLayout>
</LinearLayout>