・独自レイアウトのダイアログ
Androidでは自前で定義したレイアウトのダイアログも作成できます。
まずは、プロジェクトの[res] - [layout]にxmlファイルを追加し、レイアウトを定義します。
Graphical Layoutタブで、以下のようなレイアウトを定義しました。

xmlファイルの方はこんな感じです。
上記レイアウトのダイアログを作成するコードは次のようになります。
作成されたダイアログはこんな感じになります。

今気づいたんですが、このダイアログでYes/Noっておかしいですね。。
まずは、プロジェクトの[res] - [layout]にxmlファイルを追加し、レイアウトを定義します。
Graphical Layoutタブで、以下のようなレイアウトを定義しました。
xmlファイルの方はこんな感じです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="false"
android:orientation="vertical" >
<TextView
android:id="@+id/HeaderText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="volume control"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/MinusButton"
style="?android:attr/buttonStyle"
android:layout_width="48dp"
android:layout_height="48dp"
android:minWidth="48dip"
android:text="-" />
<SeekBar
android:id="@+id/VolumeSeekBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.93" />
<Button
android:id="@+id/PlusButton"
style="?android:attr/buttonStyle"
android:layout_width="48dp"
android:layout_height="48dp"
android:minWidth="48dp"
android:text="+" />
</LinearLayout>
</LinearLayout>
上記レイアウトのダイアログを作成するコードは次のようになります。
// ※thisはActivity自身
// xmlリソースからViewを生成するために、LayoutInflaterを取得
LayoutInflater inflater = this.getLayoutInflater();
// xmlリソースからViewを生成
View customDialogView = inflater.inflate(R.layout.custom_dialog, null);
// AlertDialog.Builder.setView()で生成したViewを設定
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customDialogView)
.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("No", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
// ダイアログを表示
builder.show();
作成されたダイアログはこんな感じになります。
今気づいたんですが、このダイアログでYes/Noっておかしいですね。。
0 件のコメント:
コメントを投稿