Android为fragment设置透明背景

问题描述 投票:0回答:12

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

我正在从 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);
}

知道怎么做吗?

android android-fragments android-activity android-support-library android-theme
12个回答
7
投票

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

不要使用

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

3
投票

创建回调并在 Acitvity 中实现它

interface OnFragmentDisplay{
  onFragmentDisplay();
}

当此片段显示时将活动背景更新为透明..或在活动中将其设置为主题

请参阅 此链接 可能会有所帮助

你试过这个吗

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

1
投票

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

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

1
投票

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


1
投票

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

android:background="@android:颜色/透明"


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

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

祝你好运。 亲切的问候, 希杰


0
投票

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

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

由于您的 Activity 有

@color/very_light_gray
,即使您将 Fragment 设置为
@android:color/transparent
也不起作用。

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


0
投票

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

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

0
投票

在代码中添加这一行

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

0
投票

将活动布局的样式设置为

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

并将其在 Activity 的父布局中设置为

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

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

会起作用的


-1
投票

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

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

-1
投票

两步:

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

在android studio中,找到“

res
”文件夹,右键单击它,
new -> 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;
}

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

© www.soinside.com 2019 - 2024. All rights reserved.