2016-12-05 195 views
0

我的主要活動具有AppBarLayout內的工具條和一個包括下面的是:包括佈局覆蓋父佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.zhephyr.somedaytoday.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimaryDark" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/activity_chrono_tab"/> 

</android.support.design.widget.CoordinatorLayout> 

所包含的佈局是一個RelativeLayout的與連接到ViewPager一個TabLayout:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".ChronoSwipeViewActivity" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorVariant" 
     android:weightSum="1"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/chrono_tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="15dp" 
      android:layout_marginEnd="15dp" 
      app:tabMode="fixed" 
      android:background="@color/colorVariant"/> 
    </LinearLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@android:drawable/ic_dialog_email" 
     android:layout_margin="@dimen/fab_margin" 
     app:fabSize="normal" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/chrono_pager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/tabs" /> 
</RelativeLayout> 

我目前遇到的問題是包含的佈局位於填充整個屏幕的主佈局的頂部。我無法看到工具欄,因爲它正在被覆蓋。我希望包含的佈局只填充工具欄下方的區域。我已經嘗試將app:layout_behavior="@string/appbar_scrolling_view_behavior"放置在兩個佈局中,並且在包含中無效。任何人都知道發生了什麼事?

更新

正在顯示的活性是這樣的:

shown activity

此活動應該這一個內,並與出應用標題中顯示:

main activity

+0

更改您的包括佈局爲'Android的根標籤:layout_height = 「WRAP_CONTENT」' – codeMagic

+0

由於CoordinatorLayout內部是FrameLayout裏。包含佈局的match_parent屬性覆蓋它。 – Vinodh

回答

0

更改包含的佈局的根目錄到android:layout_height="wrap_content"

目前,它是match_parent,所以它會佔用填充整個屏幕的父項的高度。

您還需要添加到您的<include>android:layout_below="@id/toolbar"以將其放置在工具欄下方。默認情況下,RelativeLayoutViews放在左上角,因此添加此屬性將告訴它在工具欄下面開始包含的佈局。

請注意下面的代碼中的最後兩行。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".ChronoSwipeViewActivity" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

包括主要佈局

<include layout="@layout/activity_chrono_tab" 
    android:layout_below="@id/toolbar"/> 
+0

當我嘗試這樣做時,出現一個錯誤,指出沒有找到與給定名稱相匹配的資源(位於'layout_below',值爲'@ id/toolbar')。 – Michael

+0

確保你拼寫了它在工具欄佈局中的上面聲明,但應該可以工作。你可以嘗試在這裏添加一個'+ + @ + id/toolbar',但你不需要,它不是最好的方法。也許嘗試清理你的項目。有時在進行xml更改時有必要。如果這仍然不起作用,我可以嘗試看看你的XML文件。 – codeMagic

+0

仍然沒有工作。編輯問題添加更多信息。 – Michael