且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

定制的Andr​​oid DialogFragment

更新时间:2023-01-27 18:41:41

由于您使用的是 .setTitle()方法,它只是设置与defualt设置标题如白色背景。如果您想自定义标题背景颜色,你需要有一个XML做到这一点。此外,对于DialogFragments,从我的知识和经验,你应该使用公共对话框onCreateDialog 而不是公开查看onCreateView 。这样,你返回一个对话框,而不是只是一个视图,然后您可以只需调用 .show()上,它会显示您的对话框。下面是一个例子:

Since you are using the .setTitle() method it is only setting the title with the defualt settings, such as the white background. If you want to customize the title background color you will need to have an xml to do that. Also, for DialogFragments, from my knowledge and experience, you should use public Dialog onCreateDialog instead of public View onCreateView. That way you return a Dialog as opposed to just a View that you can then just call .show() on and it will display your dialog. Here is an example:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    Bundle args = getArguments();
    currentName = args.getString(ARG_CURRENT_NAME);

    builder.setView(inflater.inflate(R.layout.name_dialog, null));
    builder.setTitle("Rename Rapper Program");
    builder.setMessage("Enter a new name for " + currentName + ":"); 
    builder.setPositiveButton("Rename", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            newName = (EditText) getDialog().findViewById(R.id.new_name);
            newProgName = newName.getText().toString();
            mRename.renameProgram(currentName, newProgName);
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dismiss();
        }
    });

    return builder.create();
}

下面是一个例子对话XML,虽然它不是在上述DialogFragment被夸大了的xml:

Here is an example dialog xml, though it is not the xml that is being inflated in the above DialogFragment:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:drawableLeft="@drawable/login"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="#FCD116"
        android:text="@string/login"
        android:textSize="36sp"/>
    <EditText android:id="@+id/username"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/un"/>
    <EditText android:id="@+id/password"
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="16dp"
        android:fontFamily="sans-serif"
        android:hint="@string/pw"/>

</LinearLayout>

的LinearLayout 正在建立的子项的其余部分进行相应的设置。第一个的TextView 充当我的头衔栏,然后在的EditText 是在对话框的身体。我对XML没有按钮,因为我设置内编程的 onCreateDialog 那些如上面code的另一片段。

The LinearLayout is setting up the rest of the child items to be placed accordingly. The first TextView acts as my "title" bar and then the EditTexts are the "body" of the dialog. I have no buttons in the xml because I set those programmatically within the onCreateDialog like in the other snippet of code above.