片段有)对替代默认的白色背景(而不是透明的片段、而不是、白色、透明

2023-09-07 17:01:28 作者:暮色年華

我在使用碎片,只是不能想出办法来面对一个奇怪的问题。

I am facing a strange problem while using fragments and just cant figure it out.

当我更换我的MainActivity容器内的碎片,有些人会保持所需的透明背景,有的会显示默认喜欢白色的。其实我是想我所有的片段是透明的,用分配给在MainActivity背景。

When I am replacing fragments inside my MainActivity container, some will keep the desired transparent background, and some will show a default-like white one. I actually want all my fragments to be transparent and 'use' the background assigned to the MainActivity.

例如:片段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>

片段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>

的main.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>

两个片段是通过使用下面的代码片段代替:

Both fragments are replaced by using following snippet:

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

所以我的问题是:

So my question is:

如何能做到这一点在我的容器中的所有片段具有透明背景,让我总是显示在MainActivity分配的背景是什么?

How can I achieve that all fragments in my container have a transparent background, so that I am always showing the background assigned in the MainActivity?

推荐答案

您可以使用以下属性为所有的根布局,视图,片段,容器等:

you can use the following property for all your root layouts, views, fragments, containers etc:

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

这应该会正常工作。

如何能做到这一点在我的容器中的所有片段具有透明背景,让我总是显示在MainActivity分配的背景是什么?

How can I achieve that all fragments in my container have a transparent background, so that I am always showing the background assigned in the MainActivity?

要只显示你的活动的背景和各方面的意见应该是透明的。

you want to display the background of your activity only and all the views should be transparent.

要做到这一点,你必须设置的android:背景=@机器人:彩色/透明每个查看比其他 MainActivity 的研究背景。对于即背景

To achieve this you have to set android:background="@android:color/transparent" for each view other than the MainActivity's backgroud. i.e background for

片段取值孩子查看取值在的ListView (S)和的ListView 的项目/童车等。 the Fragments their child Views the ListView(s) and ListView's items / childs etc.

在短每个视图必须有一个TRANSPARANT背景,使得活动的背景不重叠并且对用户可见。

In short each view must have a transparant background so that the Activities's background is not overlapped and visible to the user.

希望这有助于。