以下のようなカスタムダイアログボックスを作成したい
以下のことを試してみました。
AlertDialog.Builderのサブクラスを作成し、カスタム タイトルとカスタム コンテンツ ビューを使用してみましたが、結果は期待どおりではありませんでした。
もう 1 つの試みは、DialogFragmentをサブクラス化し、onCreateDialog 内のダイアログをカスタマイズすることでした。しかし、結果は期待どおりではありませんでした。
次に、単純なDialogクラスを使用してみました。結果は期待どおりではありませんでした。
3 つのケースすべてにおいて、問題は、タイトル ビューを見落とすとダイアログのサイズが予想どおりにならず、タイトル ビューを使用するとコンテンツ ビューの周囲に太い境界線が表示されることです (見た目が非常に悪い)。今、私の頭の中には 2 つの疑問があります...
どうすればそれを実現できるでしょうか? すでに多くのことを試したので、直接的な回答をいただければ幸いです。
Android アプリでエラーまたは警告ダイアログを表示する最適な方法は何ですか?
編集 Android 開発者向けドキュメントユーザーにエラー/警告メッセージを表示するには、DialogFragments または Dialog のいずれかを使用することを推奨しています。ただし、ある時点で次のように言っています...
ヒント: カスタム ダイアログが必要な場合は、Dialog API を使用する代わりに、アクティビティをダイアログとして表示できます。アクティビティを作成し、マニフェスト要素でそのテーマを Theme.Holo.Dialog に設定するだけです。
それはどういう意味ですか? エラーメッセージを表示するためだけにアクティビティを使用するのはやりすぎではないでしょうか???
ベストアンサー1
ここでは次のような簡単なダイアログを作成しました:
カスタムダイアログ.xml
<?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="80dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="@+id/txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Do you realy want to exit ?"
android:textColor="@android:color/white"
android:textSize="15dp"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_yes"
android:layout_width="100dp"
android:layout_height="30dp"
android:background="@android:color/white"
android:clickable="true"
android:text="Yes"
android:textColor="#5DBCD2"
android:textStyle="bold" />
<Button
android:id="@+id/btn_no"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@android:color/white"
android:clickable="true"
android:text="No"
android:textColor="#5DBCD2"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
あなたはしなければならないextends Dialog
し、implements OnClickListener
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button yes, no;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.btn_yes);
no = (Button) findViewById(R.id.btn_no);
yes.setOnClickListener(this);
no.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
c.finish();
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
}
ダイアログを呼び出す方法は?
R.id.TXT_Exit:
CustomDialogClass cdd=new CustomDialogClass(Values.this);
cdd.show();
アップデート
久しぶりに、友人の一人が透明な背景を持つ曲線形状のダイアログを作成するように頼んできました。そこで、ここでそれを実装しました。
曲線を描くには、curve_shap.XML
以下のように別途作成する必要があります。
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#000000" />
<stroke
android:width="2dp"
android:color="#ffffff" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
さて、これをcurve_shap.XML
メインビューレイアウトに追加します。私の場合はLinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@drawable/curve_shap"
android:orientation="vertical" >
...
</LinearLayout>
これをどう呼び出すか?
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
それがうまくいくことを願います。