android - 非表示 - dialogfragment 使い方
タイトルなしでDialogFragmentを作成するには? (4)
DialogFragmentを作成して、自分のアプリに関するいくつかのヘルプメッセージを表示しています。 1つのこと以外はすべてうまくいきます:ウィンドウの上部には、DialogFragmentが表示されている黒いストライプがあります。タイトルには予約されていると思われます。使用したくないものです。
私のカスタムDialogFragmentは白い背景を使用しているので、これは特に苦痛です。その変更はあまりにも残っていることで有名です。
これをよりグラフィカルに表示してみましょう:
DialogFragmentのXMLコードは次のとおりです。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/holding"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/dialog_fragment_bg"
>
<!-- Usamos un LinearLayout para que la imagen y el texto esten bien alineados -->
<LinearLayout
android:id="@+id/confirmationToast"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="@+id/confirmationToastText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="@string/help_dialog_fragment"
android:textColor="#AE0000"
android:gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/confirmationButtonLL"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
>
<Button android:id="@+id/confirmationDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="60dp"
android:background="@drawable/ok_button">
</Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
そして、DialogFragmentを実装しているクラスのコード:
public class HelpDialog extends DialogFragment {
public HelpDialog() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the XML view for the help dialog fragment
View view = inflater.inflate(R.layout.help_dialog_fragment, container);
TextView text = (TextView)view.findViewById(R.id.confirmationToastText);
text.setText(Html.fromHtml(getString(R.string.help_dialog_fragment)));
//get the OK button and add a Listener
((Button) view.findViewById(R.id.confirmationDialogButton)).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// When button is clicked, call up to owning activity.
HelpDialog.this.dismiss();
}
});
return view;
}
}
そして主な活動の作成プロセス:
/**
* Shows the HelpDialog Fragment
*/
private void showHelpDialog() {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
HelpDialog helpDialog = new HelpDialog();
helpDialog.show(fm, "fragment_help");
}
Dialogに関連するこの回答がここにも当てはまるのかどうか本当にわかりません。Android:タイトルなしでダイアログを作成するにはどうすればいいですか?
どのように私はこのタイトルエリアを取り除くことができますか?
HelpDialog.onCreateView(...)
このコード行を追加するだけHelpDialog.onCreateView(...)
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
あなたは明示的にタイトルなしのウィンドウを取得するように求めているこの方法:)
EDIT
@DataGraham
と@Blundell
は以下のコメントを指摘しているonCreateDialog()
、 onCreateDialog()
ではなくonCreateDialog()
メソッドでtitle-lessウィンドウのリクエストを追加する方が安全です。 フラグメントをDialog
として使用していないときに、このようにしてNPEを防ぐことができます:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
スタイルをTheme_Holo_Dialog_NoActionBarに設定します。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}
簡単に試してみてください
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");