Android设置片段的透明背景

22

我的应用程序只有一个活动和其他所有片段。

我通过以下方式从style.xml中设置活动的背景:

<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);
}

你有任何想法如何做到这一点吗?


想要为背景设置透明背景。 - amodkanthe
<item name="android:windowBackground">@color/very_light_gray</item> 在样式中设置,该主题应用于活动,因此所有片段的背景都是 very_light_gray,现在对于一个特定的片段,我想将背景设置为透明,但无法实现。 - amodkanthe
XML 的 background 属性应该能够完成任务...请展示您的 Fragment 和 Activity 布局。 - Adrian Grygutis
你的意思是什么?除非你使用一个可见的背景来填充布局,否则Fragment是透明的。那么,你到底想要实现什么?提供你所拥有和想要的截图可能会有所帮助。 - Eugen Pechanec
已经在填充具有透明背景的片段。 - amodkanthe
你找到解决方案了吗? - Leo DroidCoder
12个回答

7

这不是完美的解决方案,但它可行

使用DialogFragment代替Fragment

 //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
      )
    }

}

剩余的代码与您在片段中编写的代码相同

要在片段中显示,请使用以下代码;如果要在活动中显示,则使用fragmentManager

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

1
最佳解决方案是将MyFragment重命名为TransparentFragment。如下所示:open class TransparentFragment : DialogFragment() - Gilbert
它将在您的片段上方绘制灰色。 - CoolMind

3
创建回调并在Activity中实现
interface OnFragmentDisplay{
  onFragmentDisplay();
}

当这个片段显示时,将Activity背景更新为透明..或在Activity中设置主题。
查看此链接this可能会有所帮助。
你尝试过这个吗?
fragment.getView().setBackgroundColor(Color.WHITE);

1

您的Activity容器布局背景应该是透明的。然后为Fragment的布局设置背景颜色。对于特定的Fragment布局,设置透明颜色以获得所需的场景。


请再次阅读问题,我的活动具有窗口背景,我想知道如何将主题应用于片段。 - amodkanthe

1

简单的方法是为特定片段的根布局设置背景颜色,如下所示

android:background="@android:color/transparent"


请再次阅读问题,我的活动具有窗口背景,我想知道如何将主题应用于片段。 - amodkanthe

1
在样式应用中,使用 null 代表颜色。
 <item name="android:windowBackground">null</item>

请再次阅读问题,我的活动具有窗口背景,我想知道如何将主题应用于片段。 - amodkanthe

1
假设您想让活动和片段对于特定片段透明(从您的问题中并不十分清楚,但我会假设是这样),您需要在“透明”片段附加到它时将“主机”活动背景设置为透明。当任何其他片段附加到它时,您将需要重置活动背景,否则它将保持透明。
有几种方法可以做到这一点,但我会选择一些“相对”简单的方法:
在您的透明片段中:
      @Override
      public void onStart() {
        super.onStart();
// I'm using null here because drawing nothing is faster than drawing transparent pixels.
        getActivity().getWindow().setBackgroundDrawable(null);
        getView().setBackground(null);
      }

      @Override
      public void onStop() {
        super.onStop();
        getActivity().getWindow().setBackgroundDrawable(new ColorDrawable(@color/very_light_gray));
     }

不确定这是否是您想要的效果。

祝好运。 此致敬礼, CJ


0

你需要将你的Activity也设置为透明。

android:background="@android:color/transparent"

由于您的活动具有@color/very_light_gray,即使您将Fragment设置为@android:color/transparent也无效。

每个视图必须具有透明背景以避免重叠。


请再次阅读问题,我的活动具有窗口背景,我想知道如何将主题应用于片段。 - amodkanthe

0

在实例化片段时,您可以在活动中使用类似下面的代码。您可以使用自己的颜色代替Color.White。

fragment.getView().setBackgroundColor(Color.WHITE);

2
请再次阅读问题,我的活动具有窗口背景,我想知道如何将主题应用于片段。 - amodkanthe

0
在你的代码中添加这一行。
getActivity().getWindow().setBackgroundDrawableResource(R.drawable.my_drawable);

0

将您的活动布局样式设置为

<style name="RootLayout" parent="AppTheme">
    <item name="android:background">@drawable/app_transparent_background</item>
</style>

并将其设置在活动的父布局中

<LinearLayout
    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"
    style="@style/RootLayout"
    ...>

在其他你想要设置白色或其他彩色背景的片段中,应在这些片段的主布局中设置不同的样式。

它将起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接