Android自定义DialogFragment问题[重复]
Posted
技术标签:
【中文标题】Android自定义DialogFragment问题[重复]【英文标题】:Android customizing DialogFragment issue [duplicate] 【发布时间】:2020-10-30 23:23:12 【问题描述】:我正在尝试自定义 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_
android:layout_
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
。
【问题讨论】:
【参考方案1】:使用CardView
作为rounded_dialog.xml
中的容器布局。
这里是rounded_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="16dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<!--Put a Constraint Layout(or any other container) here with required views-->
</androidx.cardview.widget.CardView>
【讨论】:
你的意思是exit_dialog.xml
?
不管怎样都行不通。以上是关于Android自定义DialogFragment问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章