【问题标题】:Tab Layout covering parts of fragment, layout_below not working选项卡布局覆盖部分片段,layout_below 不起作用
【发布时间】:2016-12-16 04:22:32
【问题描述】:

所以我有一个带有 tabLayout 布局的 UserAreaActivity。每个选项卡都是它自己的片段,但是在每个片段布局中,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>

编辑:澄清所有这些都应该包含在片段布局中

【问题讨论】:

  • 将layout_below 属性放在ViewPager 上。
  • 你能分享截图吗

标签: android xml android-layout android-fragments


【解决方案1】:

正如@Mike M 指出的,在ViewPager 中添加属性“layout_below”。并为您的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>

【讨论】:

  • 非常感谢您的快速回复,您所做的工作完美!您不知道我浏览了多少其他问题和 youtube 视频来寻找答案,但找不到答案,非常感谢您!
  • 很高兴我能帮上忙 :)
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多