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;
}
}

谢谢

4

2 回答 2

2

由于您使用的是该.setTitle()方法,因此它仅使用默认设置设置标题,例如白色背景。如果要自定义标题背景颜色,则需要有一个 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充当我的“标题”栏,然后EditTexts 是对话框的“主体”。我在 xml 中没有按钮,因为我onCreateDialog在上面的其他代码片段中以编程方式设置了这些按钮。

于 2013-04-25T14:02:30.147 回答
0

如果您禁用默认窗口标题,则上述示例(TronicZomB)可能会起作用:

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

尝试一下!

于 2013-08-12T13:16:32.420 回答