2012年6月20日水曜日

[Android][入門] DialogにViewを設定する際にDialog規定のテーマを適応する

タイトルだけだと何を言ってるかわからないと思います。すみません、日本語が不得手なので。

サクッと本題。

ダイアログにレイアウトファイルから生成したビューを設定するとします。

【レイアウトファイル】
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Hello !" />
</LinearLayout>

【MyActivity クラス】
public class MyActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater =
                (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.sample, null);
        new AlertDialog.Builder(this)
        .setView(view)
        .create()
        .show();
    }
}

【AndroidManifest.xml】
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.goodfornothing.android.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Black">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


出力結果はこうなります。

そりゃそうだという感じ。

ではThemeを @android:style/Theme.Light に変更した場合はどうなるでしょうか。

【AndroidManifest.xml】
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.goodfornothing.android.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Light">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>



思ったダイアログと別物です。誰ですか。
文字色が暗すぎるため、なんだかとても見辛くなりました。

これはActivityのThemeの文字色に引きずられているためです。
AlertDialog#getLayoutInflater() より取得した LayoutInflater を通して View を生成すると、期待通りのDialogが表示されます。


【MyActivity クラス】
public class MyActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog dialog = new AlertDialog.Builder(this).create();

        LayoutInflater inflater = dialog.getLayoutInflater();
        View view = inflater.inflate(R.layout.sample, null);

        dialog.setView(view);
        dialog.show();
    }
}



期待通りですね。

0 件のコメント:

コメントを投稿