gpt4 book ai didi

android - fragment 在 replace() 上有默认的白色背景而不是透明背景

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

我在使用 fragment 时遇到了一个奇怪的问题,只是想不通。

当我替换 MainActivity 容器内的 fragment 时,一些会保持所需的透明背景,而一些会显示类似默认的白色背景。我实际上希望我的所有 fragment 都是透明的,并“使用”分配给 MainActivity 的背景。

例如: fragment A:白色背景

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="@drawable/logo"/>

<de.mypackage.uielements.MyButton
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="@string/button_text"/>

**<!-- ListView to show news -->**
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>

fragment B:透明背景

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

ma​​in.xml

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- Background Image to be set programmatically, transparent being default -->
<ImageView
android:id="@+id/window_background_image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:background="@android:color/transparent"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">

<!-- Top ScrollView -->
<de.opwoco.app.android.apptitan.app.utils.HorizontalListView
android:id="@+id/navigation_tab_listview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:visibility="gone"
android:background="@color/navigation_background"/>

<!-- FrameLayout container where all other fragments are placed -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:layout_below="@+id/navigation_tab_listview"/>

</RelativeLayout>

<fragment
android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="de.mypackage.fragment.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

两个 fragment 都使用以下 fragment 替换:

android.support.v4.app.FragmentTransaction transaction =     getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commit();

所以我的问题是:

我怎样才能使容器中的所有 fragment 都具有透明背景,以便始终显示在 MainActivity 中指定的背景?

最佳答案

您可以为所有根布局、 View 、 fragment 、容器等使用以下属性:

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

如果您只想显示 Activity 的背景,那么所有 View 都应该是透明的。

为此,除了 MainActivity 的背景之外,您必须为每个 View 设置 android:background="@android:color/transparent"。即背景

  • fragment
  • 他们的 child View
  • ListView
  • ListView的项目/ child 等。

简而言之,每个 View 都必须有一个透明的背景,这样 Activities 的背景就不会重叠并且对用户可见。

关于android - fragment 在 replace() 上有默认的白色背景而不是透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382426/

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