【问题标题】:Fragment has default white background on replace() instead of transparent one片段在 replace() 上有默认的白色背景,而不是透明背景
【发布时间】:2014-06-24 09:01:25
【问题描述】:

我在使用片段时遇到了一个奇怪的问题,但无法解决。

当我在 MainActivity 容器中替换片段时,有些会保持所需的透明背景,有些会显示类似默认的白色背景。我实际上希望我的所有片段都是透明的,并“使用”分配给 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>

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>

使用以下 sn-p 替换两个片段:

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

所以我的问题是:

如何实现容器中的所有片段都具有透明背景,以便始终显示在 MainActivity 中分配的背景?

【问题讨论】:

  • 只需从所有片段布局中删除 xml 中的背景标签,并将背景标签赋予容器(主要活动布局)..
  • @Hardik 非常感谢,但不幸的是,它不起作用。我添加了我的 main.xml 以获取更多信息

标签: android android-fragments android-listview background-image background-color


【解决方案1】:

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

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

如果您只想显示活动的背景,那么所有视图都应该是透明的。

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

  • Fragments
  • 他们的孩子Views
  • ListView(s)
  • ListView的物品/孩子等

简而言之,每个视图都必须具有透明背景,以便活动的背景不会重叠并且对用户可见。

【讨论】:

  • 非常感谢,但不幸的是,它不起作用。我添加了我的 main.xml 以获取更多信息
【解决方案2】:

在 xml 中使用:

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00E6CECE"
        android:id="@+id/fragment_container" />

在 java 中使用这个:

findViewById(R.id.fragment_container).setBackgroundColor(Color.WHITE);

【讨论】:

    【解决方案3】:

    太简单了,把背景设为#00000000就行了。

    或者转到设计选项卡并将背景设置为#00000000

    【讨论】:

      【解决方案4】:

      将背景设置为 null 或颜色/透明:

      android:background="@null"
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-27
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 2012-10-05
        相关资源
        最近更新 更多