2014-09-25 55 views
-1

我有以下活動:展會活動的看法覆蓋的FrameLayout與片段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="es.xxx.xxx.MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:background="#CCFF0000" 
     android:id="@+id/lyNetworkError"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="No hay conexión a internet" 
       android:textAlignment="center"/> 

     </LinearLayout> 

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

</RelativeLayout> 

在它的FrameLayout應用程序將加載其他片段。

這是活動的onCreate代碼

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Constants.setAppContext(this); 
     setContentView(R.layout.activity_main); 

     Log.d("LoadFragment", "1 "+ loadFragment); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit(); 
     } 
     IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); 
     registerReceiver(networkStateReceiver, filter); 

     fragmentManager = getSupportFragmentManager(); 
     lyNetworkError = (LinearLayout) findViewById(R.id.lyNetworkError); 

    } 

的問題是LinearLayout中(包含TextView中)不顯示(更多鈔票是該片段渲染過的LinearLayout,因爲如果我刪除getSupportFragmentManager().beginTransaction().replace(R.id.container, new MainFragment()).commit();的LinearLayout中出現)

那麼,如何顯示LinarLayout片段(在FrameLayout中加載)?

回答

1

如果LinearLayout和你Fragment S IN屏幕上的正確位置被顯示在每個單獨顯示,那麼你可以簡單地扭轉在你的XML的FrameLayoutLinearLayout的順序。

問題是RelativeLayout允許其孩子重疊。 RelativeLayout中的最後一項將顯示在佈局中其他項目的「上方」或「頂部」。由於您尚未爲視圖指定任何佈局約束,因此RelativeLayout會將它們都置於默認位置(左上角)。由於您的FrameLayout設置爲填充父視圖的寬度和高度,因此它將覆蓋其他所有內容。

如果你真的想LinearLayout出現在FrameLayout以上,那麼你可以使用RelativeLayout的定位屬性(explained very well here)來定位你的意見。 具體來說,你要尋找的是這樣的:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/lyNetworkError" 
    android:id="@+id/container"/> 

android:layout_below屬性告訴FrameLayout,你希望它永遠與ID lyNetworkError視圖低於(如在一張紙上,而不是文本在三維空間中)。

+0

謝謝,它的工作原理。我知道在RelativeLayout中的順序很重要。謝謝 – RdlP 2014-09-25 20:41:42