gpt4 book ai didi

android - 如何更改 DialogFragment 周围的背景颜色?

转载 作者:IT王子 更新时间:2023-10-29 00:07:56 44 4
gpt4 key购买 nike

我正在构建一个自定义 DialogFragment。对话框布局设置为 my_dialog.xml,但如何修改对话框周围的颜色(透明灰色)?

enter image description here

my_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >

<TextView
android:id="@+id/hello"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="hello" />

</RelativeLayout>

MyDialogFragment.java

public class MyDialogFragment extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.my_dialog, container);

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

return view;
}
}

最佳答案

我必须在对话框样式中将 android:windowIsFloating 设置为 false 并将 android:windowBackground 设置为我的自定义颜色:

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="MyDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@color/orange_transparent</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:gravity">center</item>
</style>

</resources>

MyDialogFragment

public class MyDialogFragment extends DialogFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialog);
}
}

关于android - 如何更改 DialogFragment 周围的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15007272/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com