gpt4 book ai didi

Android为 fragment 设置透明背景

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:48:33 36 4
gpt4 key购买 nike

在我的应用程序中,我有一个 Activity 和所有其他 fragment

我正在为 style.xml 中的 Activity 设置背景,如下所示

<item name="android:windowBackground">@color/very_light_gray</item>

现在只有一个特定的 Fragment 我想设置背景透明,但我无法做到这一点,在 Fragment 中尝试下面的代码对我不起作用

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

// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, container, false);
}

知道怎么做吗?

最佳答案

这不是完美的解决方案,但它有效

而不是使用 Fragment 使用 DialogFragment

 //exploreCategory is argument
class MyFragment(val exploreCategory: ExploreCategory) : DialogFragment() {

override fun onStart() {
super.onStart()
setWindowParams()
}

private fun setWindowParams(){
dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
}

}

剩下的代码和你在 fragment 中写的一样

在fragment中显示使用下面的代码,如果在activity中显示则使用fragmentManager

MyFragment(exploreCategory).show(childFragmentManager,null)
//exploreCategory is argument, you can choose to send no arguments

关于Android为 fragment 设置透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48974121/

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