私はカスタムダイアログを持っています。 2つのボタンを水平に近くに配置して中央に表示したい。 どうすれば実現できますか?Android:カスタムダイアログの2つのボタンを中央に表示します
はい/いいえメッセージボックスとよく似ています。
私はレイアウトXMLファイルでそれを行う方法を模索しています。
私はカスタムダイアログを持っています。 2つのボタンを水平に近くに配置して中央に表示したい。 どうすれば実現できますか?Android:カスタムダイアログの2つのボタンを中央に表示します
はい/いいえメッセージボックスとよく似ています。
私はレイアウトXMLファイルでそれを行う方法を模索しています。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:width="wrap_content"
android:height="wrap_content"
android:layout_centerHorizontal="true">
... And two buttons here
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
Android Devからは、これに似た何かを探していてもよいです。 「ポジティブ」と「ネガティブ」ボタンを設定して、あなたが望むものを言い、それぞれが独自のことをするようにすることができます。あなたの場合(そしてほとんどの場合)YesとNoはここで完璧です!
xmlファイルのいくつかの例は、私がこれまで使用していたものと同様に、そのリンクをたどって見つけることもできます。
あなたは、あなたがAlertDialogでビューとして設定するlayoutInflaterを使用し、とにかくあなたが望むダイアログのコンテンツのレイアウトを定義するためにXMLを使用することができます。
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
ドキュメントソース:http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
答えをありがとう。しかし、私はXMLレイアウト設定のみを参照していました。 – Genry
ありがとう、私はこれを試してみます。 – Genry
最後に私のために働いたのは、 'android:gracity =" center "'私は 'android:layout_centerHorizontal =" true "'の代わりに使用されています。それに応じて回答を編集する。また、アンドロイド:layout_widthとアンドロイド:layout_heightの代わりに、それぞれ、 'android:width'と' android:height'という小さなタイプミスがありました。この問題も解決しました – Genry
あなたは正しいです。私は確かではなかったし、別のパラメータを忘れてしまった。私は自宅でEclipseを持っていないし、チェックできなかった。ごめんなさい。修正されました。 – Gangnus