【问题标题】:How to customize title in DialogFragment如何在 DialogFragment 中自定义标题
【发布时间】:2016-07-14 18:46:39
【问题描述】:

我想在我的 DialogFragment 中自定义我的标题,我知道如何将标题设置为我的 DialogFragment ,但我想要的不仅仅是一个标题,我需要一个自定义的标题(颜色,字体大小......),所以我定义了一个 TextView 并自定义了它

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.height_fragment,container,false);

    TextView title =  new TextView(getContext());
    title.setText("Your Height Information");
    title.setGravity(Gravity.CENTER);
    title.setTextSize(30);
    title.setBackgroundColor(Color.GRAY);
    title.setTextColor(Color.WHITE);

    getDialog().setCustomTitle(title); //Error Can not resolve method setCustomTitle(android.widget.TextView)

    return view;
}

但是这一行有错误

getDialog().setCustomTitle(title);

getDialog().setCustomTitle(title);

在发布此问题之前我进行了很多搜索,但我找不到如何自定义我的 DialogFragment 标题。 感谢您花时间帮助我

【问题讨论】:

标签: java android android-fragments


【解决方案1】:

“getDialog”没有方法“setCustomTitle”。该方法只能用于AlertDialog.Builder。试试这样:

getDialog().setTitle("Your Height Information");
TextView textView=(TextView) getDialog().findViewById(android.R.id.title);
textView.setTextSize(30);

【讨论】:

    【解决方案2】:

    我永远无法轻松自定义我的 DialogFragment 的标题。因此,相反,我使用删除标题并将 TextView 添加到对话框布局(并像标题一样使用它)。这样,您就可以轻松地自定义您想要的方式。

    对话代码:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        // Remove TITLE
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
    
        View dialogView = dialogView = inflater.inflate(R.layout.height_fragment, null);
    
        // Do your stuff
    
        return dialogView;
    }
    

    height_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="match_parent"
            android:layout_height="56dp"
    
            <!-- THIS VIEW IS YOUR TITLE... CUSTOMIZE LYKE YOU WANT -->
            android:textColor="@color/white"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_gravity="top"
    
            android:text="@string/TITLE"
            android:gravity="center_vertical"
            android:paddingLeft="16dp" />
    
        <!-- Real Content goes below the title -->
    </FrameLayout>
    

    【讨论】:

      【解决方案3】:

      您需要将您的 xml 连接到您的班级。 findViewById 告诉你的班级使用哪个 TextView。注意它必须如何匹配 xml 中的android:id

      public class DialogFragmentTest extends DialogFragment {
      
          public final static String TITLE = "title";
          public final static String MESSAGE = "message";
      
          @Override
          public Dialog onCreateDialog(Bundle savedInstanceState) {
              AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
              // Get the layout inflater
              LayoutInflater inflater = getActivity().getLayoutInflater();
              View view = inflater.inflate(R.layout.dialog, null);
      
              // Inflate and set the layout for the dialog
              // Pass null as the parent view because its going in the dialog layout
              builder.setView(view);
      
              Bundle args = getArguments();
              String title = args.getString(TITLE, "default title");
              String msg = args.getString(MESSAGE, "default message");
      
              TextView tvTitle = (TextView)view.findViewById(R.id.dialog_title);
              tvTitle.setText(title);
      
              TextView tvMessage = (TextView)view.findViewById(R.id.dialog_message);
              tvMessage.setText(msg);
      
              Button btnDone = (Button)view.findViewById(R.id.dialog_button);
              btnDone.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      dismiss();
                  }
              });
      
              return builder.create();
          }
      }
      

      dialog.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="300dp"
          android:layout_height="wrap_content">
          <TextView
              android:id="@+id/dialog_title"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center_horizontal"
              android:text="title"/>
          <TextView
              android:id="@+id/dialog_message"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_below="@id/dialog_title"
              android:layout_marginTop="10dp"
              android:text="message"/>
          <Button
              android:id="@+id/dialog_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@id/dialog_message"
              android:layout_marginTop="20dp"
              android:layout_centerHorizontal="true"
              android:text="done"/>
      
      
      </RelativeLayout>
      

      然后从您的活动中调用:

      DialogFragmentTest bd = new DialogFragmentTest();
      Bundle b = new Bundle();
      b.putString(DialogFragmentTest.TITLE, "my title");
      b.putString(DialogFragmentTest.MESSAGE, "my message");
      bd.setArguments(b);
      bd.show(getFragmentManager(), "my title");
      

      【讨论】:

        【解决方案4】:

        请使用基于 DialogFragment 的自定义对话框:

        public class MyCustomDialog extends DialogFragment{
            private Context context;
        
            @Override
            public void onAttach(Context context) {
                super.onAttach(context);
                this.context = context;
            }
        
            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
                View dialogView = inflater.inflate(R.layout.custom_dialog, null, false);
                TextView tvTitle = (TextView) getDialog().findViewById(android.R.id.title);
                tvTitle.setTextSize(25);
                tvTitle.setTextColor(Color.GREEN);
        
                return dialogView;
            }
        
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
        
                Dialog dialog = new Dialog(this.context, R.style.CustomDialog);
                dialog.setTitle("custom dialog title");
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                return dialog;
            }
        
            public static MyCustomDialog newInstance() {
        
                MyCustomDialog myCustomDialog = new MyCustomDialog();
                return myCustomDialog;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多