【问题标题】:How to constrain view to a view in another layout file?如何将视图约束到另一个布局文件中的视图?
【发布时间】:2020-01-16 12:15:39
【问题描述】:

我的应用使用在 main_activity 布局中定义的 tabLayout。然后,每个选项卡都有一个单独的布局文件来放置该选项卡的所有视图。

在选项卡布局文件中,我想将视图限制在选项卡的底部边缘。我不知道该怎么做,因为 tabLayout 是在另一个布局文件中定义的,而且它没有固定的高度(我也不想这样),所以我不能只使用相同的尺寸在选项卡布局文件中。

我看到有一个标签可以用来从其他布局中提取内容,但这感觉不对,我不需要在标签中复制整个布局,我基本上只需要知道它的高度。

这是我的 main_activity 布局,其中包含 tabLayout。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="?attr/backgroundColour"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"     
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <android.support.design.widget.TabItem
            android:id="@+id/CostTab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cost" />

        <android.support.design.widget.TabItem
            android:id="@+id/costTab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/distance" />

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


    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/tabMenu"
        />

</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android xml android-layout android-constraintlayout


    【解决方案1】:

    您应该使用 Fragments 为您拥有的每个 tabLayout 填充 ViewPager 的适配器,或者使用普通视图作为适配器项。您可以在此处的开发者指南中找到更多信息:https://developer.android.com/guide/navigation/navigation-swipe-view

    【讨论】:

    • 我不确定我是否遵循。每个选项卡我都有片段,每个片段都有对应的布局文件。选项卡功能运行良好,只是在我的每个片段布局文件中,我不得不大致猜测选项卡的结束位置和“可用空间”的开始位置。
    • '可用空间'从fragment的布局文件的顶部开始,所以如果你想将视图约束到选项卡的底部边缘,你只需要将它约束到fragment的布局文件的顶部.如果您需要知道选项卡中布局的高度,只需使用此答案中描述的方法:stackoverflow.com/questions/16937953/…
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 2021-09-28
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    相关资源
    最近更新 更多