非表示 - Android:タイトルなしでダイアログを作成するには?
android 選択ダイアログ (16)
私はAndroidでカスタムダイアログを生成しようとしています。 私はこのように私のダイアログを作成する:
dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);
EverythingsはDialogのタイトル以外は正常に動作します。 ダイアログのタイトルを設定しなくても、ダイアログのポップアップにはダイアログの位置に空白があります。
ダイアログのこの部分を隠す方法はありますか?
AlertDialogで試してみましたが、レイアウトが正しく設定されていないようです:
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);
dialog = builder.create();
((TextView) dialog.findViewById(R.id.nr)).setText(number);
このコードを使用すると、最後の行にnullポインタ例外が発生します。 ダイアログはnullではないので、取り出そうとしているTextViewは存在しません。
私がDialog Constructorを使用する部分のコメントを外すと、すべてうまくいきますが、私のダイアログレイアウトの上にあるタイトルのために機能します。
AlertDialogの使用中にsetTitle setTitle()
を使用しsetTitle()
と、タイトルが消えます
FEATURE_NO_TITLEは、次のように、最初からダイアログを作成するときに機能します。
Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
しかし、AlertDialogを作成するとき(またはBuilderを使用するとき)は、すでにタイトルを無効にしていて、カスタムのものを内部的に使用するため、このメソッドは機能しません。
私はSDKのソースを見てきましたが、それは回避できないと思います。 したがって、上のスペーシングを削除するには、Dialogクラスを直接使用して、スクラッチIMOからカスタムダイアログを作成することが唯一の解決策です。
また、スタイルを使用してそれを行うこともできます(styles.xmlなど)。
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
その後:
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
AlertBuilder
タイトルを消すためにできることはAlertBuilder
です:
TextView title = new TextView(this);
title.setVisibility(View.GONE);
builder.setCustomTitle(title);
AlertDialog
使わずにDialog
クラスからAlertDialog
する新しいClassを定義することでこれを行うことができます:
public class myDialog extends Dialog {
public myDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
setcontentview
前に以下のコードを使用してください:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
注 :上記のコードは、同じ順序で並んでいる必要があります。 requestWindowFeature
はsetContentView行の前にある必要があります。
oliviergの答えは私のために働いていて、カスタムDialogクラスを作成することがあなたが行きたいと思っているならば最高の解決策です。 しかし、私はAlertDialogクラスを使用できないと悩ましました。 私は、デフォルトのシステムAlertDialogスタイルを使用できるようにしたかったのです。 カスタムダイアログクラスを作成してもこのスタイルはありません。
だから私はカスタムクラスを作成せずに動作するソリューション(ハック)を発見した、あなたは既存のビルダーを使用することができます。
AlertDialogは、コンテンツビューの上にあるビューをタイトルのプレースホルダとして配置します。 ビューを見つけて高さを0に設定すると、スペースがなくなります。
これまで2.3と3.0でこれをテストしましたが、それはまだすべてのバージョンで動作しない可能性があります。
それを行うための2つのヘルパーメソッドがあります:
/**
* Show a Dialog with the extra title/top padding collapsed.
*
* @param customView The custom view that you added to the dialog
* @param dialog The dialog to display without top spacing
* @param show Whether or not to call dialog.show() at the end.
*/
public static void showDialogWithNoTopSpace(final View customView, final Dialog dialog, boolean show) {
// Now we setup a listener to detect as soon as the dialog has shown.
customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Check if your view has been laid out yet
if (customView.getHeight() > 0) {
// If it has been, we will search the view hierarchy for the view that is responsible for the extra space.
LinearLayout dialogLayout = findDialogLinearLayout(customView);
if (dialogLayout == null) {
// Could find it. Unexpected.
} else {
// Found it, now remove the height of the title area
View child = dialogLayout.getChildAt(0);
if (child != customView) {
// remove height
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();
lp.height = 0;
child.setLayoutParams(lp);
} else {
// Could find it. Unexpected.
}
}
// Done with the listener
customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
// Show the dialog
if (show)
dialog.show();
}
/**
* Searches parents for a LinearLayout
*
* @param view to search the search from
* @return the first parent view that is a LinearLayout or null if none was found
*/
public static LinearLayout findDialogLinearLayout(View view) {
ViewParent parent = (ViewParent) view.getParent();
if (parent != null) {
if (parent instanceof LinearLayout) {
// Found it
return (LinearLayout) parent;
} else if (parent instanceof View) {
// Keep looking
return findDialogLinearLayout((View) parent);
}
}
// Couldn't find it
return null;
}
使用方法の例を次に示します。
Dialog dialog = new AlertDialog.Builder(this)
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, true);
これをDialogFragmentで使用する場合は、DialogFragmentのonCreateDialog
メソッドをオーバーライドします。 次に、上の最初の例のようにダイアログを作成して返します。 唯一の変更点は、ダイアログボックスでshow()を呼び出さないように、falseを3番目のパラメータ(show)として渡す必要があることです。 DialogFragmentはそれを後で処理します。
例:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new AlertDialog.Builder(getContext())
.setView(yourCustomView)
.create();
showDialogWithNoTopSpace(yourCustomView, dialog, false);
return dialog;
}
これをさらにテストする際には、必要な追加調整があれば必ず更新してください。
あなたのコードでこの行を追加してください
requestWindowFeature(Window.FEATURE_NO_TITLE);
またはXMLでテーマを使用する
android:theme="@android:style/Theme.NoTitleBar"
タイトルバーが作成されてから削除されるコードバージョンと同じように、XMLがより優れた実装になります。これはリソースの無駄です
それは動作していません。 私は取得:android.view.WindowManager $ BadTokenException:ウィンドウを追加することができません - ダイアログをshwoしたい場合は、アプリケーションのトークンnullではありません。
アラートダイアログのタイプをシステムダイアログ(TYPE_SYSTEM_OVERLAYなど)に変更し、問題が解決するかどうかを確認してください
あなたのコードでは、 requestWindowFeature(Window.FEATURE_NO_TITLE);
それがdialog.setContentView();
前にあることを確認してくださいdialog.setContentView();
それ以外の場合は、アプリケーションがクラッシュします。
このように使う:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
これにより、ダイアログウィンドウからタイトルバーが削除されます。
これを使って
Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.setCancelable(true);
dialog.setContentView(R.layout.image_show_dialog_layout);
ハッキングの束の後、私はこれを働かせる:
Window window = dialog.getWindow();
View view = window.getDecorView();
final int topPanelId = getResources().getIdentifier( "topPanel", "id", "android" );
LinearLayout topPanel = (LinearLayout) view.findViewById(topPanelId);
topPanel.setVisibility(View.GONE);
ビルダを使用してタイトルを空の文字列に設定します。
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("");
...
builder.show();
以下を使用してダイアログのタイトルを非表示にすることができます。
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
この回答の以前のバージョンは、過度に複雑です。
AlertDialog
を使用する必要があります。 カスタムのダイアログについては、Androidデベロッパーのサイトに詳しい説明があります 。
非常に短い要約では、公式のウェブサイトから以下のようにコピーしてください。 それはカスタムレイオファイルを取り込み、それを膨らませ、基本的なテキストとアイコンを与え、それを作成します。 alertDialog.show()
でそれを表示します。
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();
コメントに応じて:
私はTextViewとID nr
がView view = inflater....
膨張しているView view = inflater....
。 そうであれば、 dialog.findView...
代わりにちょうど1ビットを変更する必要がありview.findView...
。それをview.findView...
。 そしてそれをしたら、builder.create()をすることなく、dialog.show()またはbuilder.show()を使用してください。
私はこの質問がまだ実際であるかどうかはわかりませんが、私の場合DialogからDialogFragmentに切り替えたとき、
requestWindowFeature(Window.FEATURE_NO_TITLE);
オプションではありませんでしたが、
setStyle(STYLE_NO_TITLE, 0);
その代わりに同じ結果が得られます。
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
タイトルの少ないダイアログを作成する
dialog=new Dialog(YourActivity.this, 1); // to make dialog box full screen with out title.
dialog.setContentView(layoutReference);
dialog.setContentView(R.layout.layoutexample);