【问题标题】:Android set transparent background for a fragmentAndroid为片段设置透明背景
【发布时间】:2018-02-25 13:43:02
【问题描述】:

在我的应用中,我有单个活动和所有其他片段

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

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

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

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

知道怎么做吗?

【问题讨论】:

  • 想给一个背景设置透明背景
  • ​​@color/very_light_gray 设置了样式,并且该主题应用于活动,因此所有片段都有背景very_light_gray,现在用于特定片段我想要设置背景透明无法做到这一点
  • xml 的背景属性应该可以工作...请向我们展示您的片段和活动布局
  • 什么意思?片段是透明的,除非你用可见的背景膨胀布局。那么,您到底想达到什么目的?您拥有什么和想要什么的屏幕截图可能会有所帮助。
  • 已有透明背景的充气片段

标签: android android-fragments android-activity android-support-library android-theme


【解决方案1】:

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

不要使用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
      )
    }

}

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

为了在片段中显示使用下面的代码,如果在活动中显示则使用fragmentManager

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

【讨论】:

  • 最佳解决方案。应该从 MyFragment 重命名为 TransparentFragment。如open class TransparentFragment : DialogFragment()
【解决方案2】:

创建一个回调并在 Acitvity 中实现它

interface OnFragmentDisplay{
  onFragmentDisplay();
}

当这个片段显示更新活动背景为透明..或在活动中设置主题

请参阅 this linkthis 可能会有所帮助

你试过了吗

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

【讨论】:

    【解决方案3】:

    在样式应用中使用 null 作为颜色

     <item name="android:windowBackground">null</item>
    

    【讨论】:

    • 请再次阅读问题我的活动有窗口背景我想知道如何将主题应用于片段
    【解决方案4】:

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

    【讨论】:

    • 请再次阅读问题我的活动有窗口背景我想知道如何将主题应用于片段
    【解决方案5】:

    因此,假设您希望活动和片段对于特定片段是透明的(不清楚这是否是您想要的问题,但我会假设它是)..您需要设置当“透明”片段附加到它时,“主机”活动背景变为透明。当任何其他片段附加到它时,您将需要重置活动背景,否则它将保持透明..

    有几种方法可以做到这一点,但我会选择一些“相当”简单的方法:

    在你的透明片段中:

          @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

    【讨论】:

      【解决方案6】:

      您必须将您的活动也设置为透明的。

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

      由于您的活动有@color/very_light_gray,即使您将Fragment 设置为@android:color/transparent,它也不起作用。

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

      【讨论】:

      • 请再次阅读问题我的活动有窗口背景我想知道如何将主题应用于片段
      【解决方案7】:

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

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

      【讨论】:

      • 请再次阅读问题我的活动有窗口背景我想知道如何将主题应用于片段
      【解决方案8】:

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

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

      【讨论】:

      • 请再次阅读问题我的活动有窗口背景我想知道如何将主题应用于片段
      【解决方案9】:

      在代码中添加这一行

      getActivity().getWindow().setBackgroundDrawableResource(R.drawable.my_drawable);
      

      【讨论】:

        【解决方案10】:

        将活动布局的样式设置为

        <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"
            ...>
        

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

        会有用的

        【讨论】:

          【解决方案11】:

          您刚刚为下面代码的布局片段设置了背景:

          <LinearLayout
          ...
          android:background="@android:color/transparent"/>
          

          【讨论】:

          • 请再次阅读问题我的活动有窗口背景我想知道如何将主题应用于片段
          【解决方案12】:

          两步:

          第一步 - 创建一个具有透明背景的视图(在 XML 文件中)。

          在android studio中,找到“res”文件夹,右键单击它,new -&gt; android resource file。比在出现的对话框中输入:

          File name: {把任何东西放在这里。例如 - "my_fragment" (不带引号)} 注意只有小写字母、数字和下划线是可以的

          Resource type: 布局

          Root element: LinearLayout(你可以在这里放任何东西,等你学会了更好地了解 XML 布局)

          Source set:

          Directory name: 布局。

          在将出现的窗口中切换到“文本”模式而不是“设计”(查找屏幕左下方的选项卡)。

          而不是复制粘贴这个而不是文件的内容(非常相似,我们只是在这里添加“透明”背景):

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout 
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" 
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@android:color/transparent"
              >
          
          </LinearLayout>
          

          第二步 - 在片段中扩充此布局。

          创建扩展 Fragment 的片段类并覆盖 onCreateView() 方法,在该方法中膨胀您的视图并返回它:

          public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
              // assuming you named your xml file "my_fragment":
              // for other names replace R.layout.my_fragment with  R.layout.put_your_name_here
              View view = inflater.inflate(R.layout.my_fragment, container, false);
              return view;
          }
          

          当然,不要忘记在活动中创建片段的实例!

          【讨论】:

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