由于您使用的是.setTitle()
方法是只设置与defualt设置,标题如白色背景。如果你想自定义标题背景颜色,你需要有一个XML来做到这一点。另外,对于DialogFragments,根据我的知识和经验,您应该使用public Dialog onCreateDialog
而不是public View onCreateView
。这样,你返回一个对话框,而不仅仅是一个视图,你可以打电话.show()
,它会显示你的对话框。下面是一个例子:
@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:
<?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
之内。
// Remove the title
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
试试:
did you refer [developer.android.com](http://developer.android.com/reference/android/app/DialogFragment.html)他们给出了一个很容易学习的例子 – Gunaseelan 2013-04-25 13:58:53