2014-10-10 90 views
8

假設我有2個片段,一個包含列表視圖,另一個包含加載文本。我希望當我點擊一個列表項時,加載文本片段出現在列表視圖的頂部。我已經將加載文本背景的不透明度調整爲:android:background =「#33FFFFFF」。但它仍然顯示在純灰色背景上加載文本。Android:片段覆蓋另一個半透明片段

<?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"/> 

片段包含一個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" /> 

我的Java代碼基本上是這樣的:onItemClick:

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

回答

11

我做到了,它完美的作品,但使用的。秀我用。新增:

活動佈局:

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

在活動:

首先添加您的列表視圖:

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

然後加載文本:

// 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(); 

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

而your_fragment_layout.xml有一個沒有任何特殊屬性的常見TextView。

希望它有用,

問候!

+0

什麼ListViewFragment,是它從這個https://github.com/ribot/easy-adapter? – ishandutta2007 2017-09-12 03:20:19

+0

其次我得到錯誤'fragmentManager.beginTransaction()調用需要API 11或之前',所以請提供版本。 – ishandutta2007 2017-09-12 03:36:37

+0

嗨!不,ListViewFragment只是我LisView的名稱(我沒有代碼[2014],但沒有特別的行爲。關於所需的API級別,請嘗試使用支持庫。關注! – 2017-09-25 12:13:52

1

我認爲你實際上是替換原始片段而不是覆蓋它。您應該創建兩個framelayouts覆蓋整個屏幕,然後分配負載片段的疊加框架佈局

在你活動

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

,然後加載它們

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

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

不能使用一幀佈局,只需將兩個其他容器佈局添加到視圖中,然後每個渲染片段? – jonney 2015-04-15 13:20:38

+1

不是,框架佈局是動態添加片段的最佳選擇。 – Kelly 2015-04-16 13:40:12