我需要为DialogFragment
添加自定义标题。
getDialog().setTitle("My Title");
对我来说还不够,因为我想更改标题文字和背景颜色。
正如您从下图所示,getDialog().setTitle("My Title");
我没有自定义(据我所知),即文字颜色为黑色,背景为白色。
我的目标是My Title
自定义Custom Title
。我一直在寻找API,我找不到任何方法将视图加载到标题部分。
任何人都知道如何在FragmentDialog
?
答案 0 :(得分:8)
您必须通过覆盖onCreateDialog来创建自定义对话框,如下所示:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dialogView = inflater.inflate(R.layout.dialog_change_password, null);
/* Anything that needs to be done to the view, ie.
on click listeners, postivie/negative buttons,
values being changed etc */
builder.setView(dialogView);
return builder.create();
}
和dialog_change_password.xml:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white_grey">
<TextView
android:text="@string/change_password"
android:id="@+id/title"
style="@style/dialogHeading" />
<LinearLayout
android:layout_marginTop="10dp"
style="@style/dialogSection">
<LinearLayout
style="@style/PasswordChangeRow">
<TextView
style="@style/PasswordChangeLabel"
android:text="@string/new_password"/>
<EditText
style="@style/PasswordChangeField"
android:id="@+id/password"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
style="@style/PasswordChangeRow">
<TextView
style="@style/PasswordChangeLabel"
android:text="@string/confirm_password"/>
<EditText
style="@style/PasswordChangeField"
android:id="@+id/confirmPassword"
android:inputType="textPassword"/>
</LinearLayout>
</LinearLayout>
<TextView
android:text="@string/password_conditions_message"
style="@style/dialogMessage" />
</LinearLayout>
显然xml有点复杂,需要什么,但只是一个可以做的事情的例子,它看起来像这样
答案 1 :(得分:2)
您可以使用setCustomTitle
方法,在那里您可以为自己的完全自定义布局充气
实施例
import android.support.v4.app.DialogFragment;
public class PromoDetailDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
View titleView = getActivity().getLayoutInflater().inflate(R.layout.my_title, null);
TextView tv = (TextView) titleView.findViewById(R.id.my_title_textView);
tv.setText("title");
AlertDialog dialog = new AlertDialog.Builder(getContext())
.setCustomTitle(titleView)
.setMessage("message")
.setCancelable(true)
.create();
return dialog;
}
}
这种方式更清洁,更强大