标签布局覆盖片段的一部分,layout_below不起作用

时间:2016-12-16 04:22:32

标签: android xml android-layout android-fragments

所以我有一个UserBreaActivity,其布局包含tabLayout。每个选项卡都是自己的片段,但是在每个片段布局中,tabLayout都覆盖了片段布局的顶部。我已经尝试使用android:layout_below并给它tabLayout的id,但它无法正常工作。这是我的片段代码:

<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"
android:layout_below="@+id/tabLayout"
tools:context="com.example.aleksandr.godspeedinvestments.OfferFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:text="After Repair Value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/afRepValTV" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/afRepValueET"
        android:hint="After Repair Value" />

    <TextView
        android:text="Closing Cost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cc1TV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cc1OutTV" />

    <TextView
        android:text="Agents Fee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/afTV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/afOutTV" />

    <TextView
        android:text="Repairs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/repTV" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/repET"
        android:hint="Repairs" />

    <View
        android:id="@+id/SplitLine_hor1"
        android:layout_width="match_parent"
        android:layout_height= "2dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:text="Net"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/netTV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/netOutTV" />

    <TextView
        android:text="Closing Cost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cc2TV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cc2OutTV" />

    <TextView
        android:text="Holding Cost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/holdCostTV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/holdCostOutTV" />

    <TextView
        android:text="Profit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profitTV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profitOutTV" />
    <View
        android:id="@+id/SplitLine_hor2"
        android:layout_width="match_parent"
        android:layout_height= "2dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:text="Offer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/offerTV" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/offerOutTV" />
    </LinearLayout>

</RelativeLayout>

这是我的UserAreaActivity.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_user_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aleksandr.godspeedinvestments.UserAreaActivity">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabTextAppearance="@style/MyTabStyle"
        ></android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewPager"
    ></android.support.v4.view.ViewPager>
</RelativeLayout>

编辑:澄清所有这些应该包含在片段布局This is what should be included in the activity

However, as you can see the tab layout is blocking the top of the fragment from being displayed

2 个答案:

答案 0 :(得分:4)

正如@Mike M指出的那样,添加属性&#34; layout_below&#34;在ViewPager中。并为您的AppBarLayout提供ID。您的UserAreaActivity.xml将如下所示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_user_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.aleksandr.godspeedinvestments.UserAreaActivity">
    <android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/toolbarLayout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
        <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
       />
       <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:tabTextAppearance="@style/MyTabStyle"
        ></android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

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

答案 1 :(得分:0)

将属性app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到ViewPager对我来说很有效。 如果它不在您的字符串文件中,则值为android.support.design.widget.AppBarLayout$ScrollingViewBehavior