自定义 Android DialogFragment

3

我是一名初级 Android 程序员。现在我正在开发一个小型应用程序,并拥有一个 DialogFragment。一切都运作得很完美,它也能显示对话框了。但是我遇到了一些颜色方案上的困难。我已经更改了布局的背景颜色,但其标题栏颜色仍然是白色,标题文本颜色是蓝色,下面还有一条蓝线(需要将其更改为绿色)。我该如何实现这一点呢?请帮帮我。

以下是我的片段代码:

public class ClientInfofrag extends DialogFragment {

public ClientInfofrag()
{

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.clientinfolayout, container);
     getDialog().setTitle("Client Info");

     return view;
}
}

谢谢你


1
你是否参考了developer.android.com?他们提供了一个非常易学的例子。 - Gunaseelan
2个回答

2

由于您正在使用 .setTitle() 方法,它只设置了标题的默认设置,例如白色背景。如果您想自定义标题背景颜色,则需要使用xml来实现。此外,根据我的知识和经验,对于DialogFragments,您应该使用 public Dialog onCreateDialog 而不是 public View onCreateView。这样您就可以返回一个 Dialog,而不仅仅是一个 View,然后您可以调用 .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中像上面的代码片段一样在程序中设置了这些按钮。


它似乎可以工作。我仍然有对话框名称和默认背景。 - jiahao
@jiahao 你是否使用了.setTitle来设置对话框的名称?那么它当然仍然是默认值。在XML中创建标题,只需不使用.setTitle即可。 - TronicZomB
问题是:“我已经更改了布局的背景颜色,但其标题栏颜色仍然是白色,标题文本颜色为蓝色,下面有一条蓝线(需要将其更改为绿色)。我该如何实现这一点?请帮帮我。”而您所做的只是设置要显示的字符串。您没有对背景做任何处理。您尝试在设备上运行它了吗?(您已经填充了一个包含自己标题的视图,这不是对话框的标题)。可能的解决方案之一是使用选项禁用窗口的标题。 - jiahao

0

如果您禁用默认的Windows标题,则上述示例(TronicZomB)可以正常工作:

// Remove the title
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

试一下!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接