Programming

제목없이 DialogFragment를 만드는 방법은 무엇입니까?

procodes 2020. 2. 29. 15:37
반응형

제목없이 DialogFragment를 만드는 방법은 무엇입니까?


내 앱과 관련된 도움말 메시지를 표시하기 위해 DialogFragment를 만들고 있습니다. 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");
}

대화 상자와 관련된이 답변이 Android에 맞는지 모르겠습니다 . 제목없이 대화 상자를 만드는 방법은 무엇입니까?

이 타이틀 영역을 제거하려면 어떻게해야합니까?


이 코드 줄을 HelpDialog.onCreateView(...)

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

이렇게하면 제목없이 창을 명시 적으로 요청합니다 :)


편집하다

으로 @DataGraham하고 @Blundell아래의 의견에 지적, 그것은에 제목없는 창에 대한 요청을 추가하는 더 안전 onCreateDialog()대신 방법 onCreateView(). 이렇게하면 조각을 다음과 같이 사용하지 않을 때 성가신 NPE를 피할 수 있습니다 Dialog.

@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;
}

대화 상자 조각에는 setStyle 메소드가 있으며,보기 작성 Java Doc 전에 호출해야합니다 . 또한 같은 방법으로 대화 상자의 스타일을 설정할 수 있습니다

public static MyDialogFragment newInstance() {
        MyDialogFragment mDialogFragment = new MyDialogFragment();
        //Set Arguments here if needed for dialog auto recreation on screen rotation
        mDialogFragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
        return mDialogFragment;
}

FragmentManager manager = getSupportFragmentManager();
SettingsDialog sd = new SettingsDialog();
sd.setStyle(DialogFragment.STYLE_NO_TITLE, 0);
sd.show(manager, "settings_dialog");

쉬운 방법을 시도

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, 0);
}

public class LoginDialog extends DialogFragment {   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login_dialog, null);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return view;
    }   
}

스타일을 Theme_Holo_Dialog_NoActionBar로 설정하십시오.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog_NoActionBar);
}

참고 URL : https://stackoverflow.com/questions/15277460/how-to-create-a-dialogfragment-without-title



반응형