Android:片段用半透明覆盖另一个片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26300674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:13:03  来源:igfitidea点击:

Android: Fragment overlay another fragment with semi transparent

androidandroid-fragments

提问by Great Question

Let's say I have 2 fragments, one contain a list view and another contain a loading text. I want when I click on one list item, the loading text fragment appears on top of the list view. I have adjusted the opacity of the loading text background to: android:background="#33FFFFFF" . But still it just shows the loading text on a solid grey background.

假设我有 2 个片段,一个包含列表视图,另一个包含加载文本。我希望当我单击一个列表项时,加载文本片段会出现在列表视图的顶部。我已将加载文本背景的不透明度调整为: android:background="#33FFFFFF" 。但它仍然只是在纯灰色背景上显示加载文本。

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/list"
    android:background="#e8e9ee"
    android:divider="#ccc"
    android:dividerHeight="1dp"/>

Fragment that contains a textview:

包含文本视图的片段:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/loadingText"
    android:id="@+id/loadingText"
    android:textColor="#e8e9ee"
    android:background="#33FFFFFF"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

My java code is basically something like this: onItemClick:

我的java代码基本上是这样的:onItemClick:

 FragmentTransaction transaction=manager.beginTransaction();
 transaction.show(loadingFragment);
 transaction.commit();

回答by Nahuel Barrios

I did it and it works perfectly but instead of using .show I used .add:

我做到了,而且效果很好,但我没有使用 .show,而是使用了 .add:

Activity layout:

活动布置:

<RelativeLayout
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginRight="5dp"
    android:layout_marginEnd="5dp">

    <FrameLayout
        android:id="@+id/list_view_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/loading_text_fragment_container"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </FrameLayout>

</RelativeLayout>

In the Activity:

在活动中:

First add your List View:

首先添加您的列表视图:

ListViewFragment listViewFragment = new ListViewFragment();
fragmentManager.beginTransaction().add(R.id.list_view_container, listViewFragment).commit();

Then the loading text:

然后加载文本:

// Create a new Fragment to be placed in the activity layout
YourFragment yourFragment = new YourFragment();

// Add the fragment to the 'loading_text_fragment_container' FrameLayout
fragmentManager.beginTransaction().add(R.id.loading_text_fragment_container, yourFragment).commit();

In YourFragment.java

在 YourFragment.java 中

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.your_fragment_layout, container, false);
}

And the your_fragment_layout.xml has a common TextView without any special attribute.

而 your_fragment_layout.xml 有一个没有任何特殊属性的通用 TextView。

Hope it to be useful,

希望有用,

regards!

问候!

回答by Kelly

I think you are actually replacing the original fragment rather than overlaying it. You should create two framelayouts that cover the full screen and then assign the loading fragment to the overlaying frame layout

我认为您实际上是在替换原始片段而不是覆盖它。您应该创建两个覆盖全屏的框架布局,然后将加载片段分配给覆盖的框架布局

in you activity

在你的活动中

<FrameLayout id="@+id/ListFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout id="@+id/LoadingFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
</FrameLayout>

and then to load them

然后加载它们

FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.ListFragment, listFragment);
transaction.commit();

FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.LoadingFragment, loadingFragment);
transaction.commit();