Android DialogFragment - 如何以编程方式更改带有圆角的对话框片段的颜色
Android DialogFragment - How to change color of dialog fragment with rounded corners programmatically
对此进行了相当多的调查,但我对图层列表的体验仍然很差。这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dip"/>
<padding
android:bottom="2dip"
android:left="8dip"
android:right="8dip"
android:top="2dip"/>
</shape>
</item>
<item
android:id="@+id/dialog_bg">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke
android:width="16dip"
android:color="#FFFFFF"/>
<padding
android:bottom="2dip"
android:left="8dip"
android:right="8dip"
android:top="2dip"/>
</shape>
</item>
</layer-list>
我对如何执行此操作有正确的想法吗?
我想使用这个 java 方法:
getDialog().getWindow().setBackgroundDrawableResource(R.drawable.rounded_corners_dialog);
但是我将如何更改它的颜色,稍后在代码中?
谢谢,
T
您可以将 CardView
作为自定义 DialogFragment 布局的父容器。这是一个简单的例子:
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:cardCornerRadius="8dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
//Put all your views here
</LinearLayout>
</android.support.v7.widget.CardView>
现在在你的 class 中扩展 DialogFragment
覆盖 onCreateDialog
像这样:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
现在您可以稍后制作圆角并更改 CardView
的背景颜色。试一试:)
对此进行了相当多的调查,但我对图层列表的体验仍然很差。这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="10dip"/>
<padding
android:bottom="2dip"
android:left="8dip"
android:right="8dip"
android:top="2dip"/>
</shape>
</item>
<item
android:id="@+id/dialog_bg">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke
android:width="16dip"
android:color="#FFFFFF"/>
<padding
android:bottom="2dip"
android:left="8dip"
android:right="8dip"
android:top="2dip"/>
</shape>
</item>
</layer-list>
我对如何执行此操作有正确的想法吗?
我想使用这个 java 方法:
getDialog().getWindow().setBackgroundDrawableResource(R.drawable.rounded_corners_dialog);
但是我将如何更改它的颜色,稍后在代码中?
谢谢,
T
您可以将 CardView
作为自定义 DialogFragment 布局的父容器。这是一个简单的例子:
custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:cardCornerRadius="8dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
//Put all your views here
</LinearLayout>
</android.support.v7.widget.CardView>
现在在你的 class 中扩展 DialogFragment
覆盖 onCreateDialog
像这样:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
现在您可以稍后制作圆角并更改 CardView
的背景颜色。试一试:)