我正在尝试自定义布局,DialogFragment
我的想法是创建一个圆角对话框,按钮文本颜色设置为colorPrimary
:
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
public class ExitDialogFragment extends DialogFragment {
ExitDialogListener listener;
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_exit, null))
.setMessage(R.string.exit_dialog_title)
.setPositiveButton(R.string.confirm_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onDialogPositiveClick(ExitDialogFragment.this);
}
})
.setNegativeButton(R.string.back_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
listener.onDialogNegativeClick(ExitDialogFragment.this);
}
});
return builder.create();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try {
listener = (ExitDialogListener) context;
} catch (Throwable e) {
throw new ClassCastException("Must implement NoticeDialogListener");
}
}
public interface ExitDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
}
}
对话框布局 ( dialog_exit.xml
) 是这样定义的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_dialog"
android:orientation="vertical"
android:theme="@style/DialogTheme">
</LinearLayout>
这里我定义了DialogTheme
( styles.xml
):
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="Launcher" parent="AppTheme">
<item name="android:windowBackground">@drawable/launch_screen</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="colorControlNormal">@color/colorPrimary</item>
<item name="colorControlActivated">@color/colorPrimary</item>
</style>
<style name="UriEditText" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/text_color_clear</item>
<item name="colorControlActivated">@color/text_color_clear</item>
</style>
</resources>
这是对话框背景(rounded_dialog.xml
):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<corners android:radius="20dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
没有应用任何内容。对话框只是矩形,按钮颜色设置为AppTheme
colorAccent
。