当我们显示DialogFragment时,如何更改背景的前景颜色,例如在Evernote或Messenger应用程序中单击fab时。我知道实现这种效果的方法可能会有所不同,但这是我要寻找的效果。
当我们显示DialogFragment时,如何更改背景的前景颜色,例如在Evernote或Messenger应用程序中单击fab时。我知道实现这种效果的方法可能会有所不同,但这是我要寻找的效果。
我已找到了解决这个问题的方法。下面是代码。 在styles.xml中设计自定义主题。
styles.xml
<style name="AppTheme.CustomTheme">
<item name="android:windowIsFloating">true</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<activity android:name=".DialogDesign"
android:theme="@style/AppTheme.CustomTheme"
android:screenOrientation="portrait"></activity>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_width="match_parent"
android:layout_height="match_parent"
tools:context=".DialogDesign">
<RelativeLayout
android:id="@+id/fadedBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7fffffff"
android:layout_margin="0dp"/>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="500dp"
android:layout_alignParentBottom="true"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_centerHorizontal="true"
android:background="@android:color/white"/>
</RelativeLayout>
public class DialogDesign extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog_design);
RelativeLayout fadedBg=(RelativeLayout)findViewById(R.id.fadedBg);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = 1800;
params.width = 1100;
this.getWindow().setAttributes(params);
}
}
完成了!在最内层的相对布局中添加任何想要添加的内容。
浮动窗口(例如对话框)后面的内容变暗的程度由您的主题中的 backgroundDimAmount
属性确定:
<style name="AppTheme" parent="@style/Theme.AppCompat">
<item name="android:backgroundDimAmount">0.6</item>
</style>
它仅限于黑色。默认值为0.6
。
上面的答案不起作用,看起来该值需要应用于对话框主题而不是活动主题。
<style name="AppTheme" parent="@style/Theme.AppCompat">
<item name="alertDialogTheme">@style/AppTheme.Dialog.Alert</item>
</style>
<style name="AppTheme.Dialog.Alert" parent="@style/Theme.AppCompat.Dialog.Alert">
<item name="android:backgroundDimAmount">0.6</item>
</style>
要实现不同的颜色,需要禁用此系统暗化并且大量定制对话框背景。我会把这留给其他人。
AlertDialog
,请提供一个dialogTheme
属性/样式组合。 - Eugen Pechanec<style name="MyCustomTheme" parent="@android:style/Theme.Panel">
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.7</item>
</style>
首先启用一个名为 'backgroundDimEnabled' 的属性,以使屏幕前景在显示对话框时变暗。然后设置浮点值以表示变暗的程度。(0 < value < 1)
在创建对话框片段的 onCreateDialog 方法中使用此主题。
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyCustomTheme);
View addDialogView = getActivity().getLayoutInflater().inflate(R.layout.word_detail_fragment, null);
builder.setView(addDialogView);
return builder.create();
}