如何使用ConstraintLayout以百分比设置视图高度

时间:2019-10-10 05:11:09

标签: android android-layout android-constraintlayout constraintlayout-guideline

目前我正在学习ConstraintLayout

下面是到目前为止我尝试过的代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="#000000"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".activity.PhaseListActivity">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.8"
                android:background="@color/colorAccent">

                <VideoView
                    android:id="@+id/myVideoView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:visibility="visible" />

                <TextSwitcher
                    android:id="@+id/tvAnimText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center"
                    android:fontFamily="@font/knowledge_bold"
                    android:textColor="@color/greenColor"
                    android:textSize="@dimen/_100ssp"
                    android:textStyle="bold"
                    tools:text="1" />

            </FrameLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Scrollbale view below my videoView"
                    android:textColor="#ff00"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Scrollbale view below my videoView"
                    android:textColor="#ff00"
                    android:textStyle="bold" />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="Scrollbale view below my videoView"
                    android:textColor="#ff00"
                    android:textStyle="bold" />


            </LinearLayout>


        </LinearLayout>


    </androidx.core.widget.NestedScrollView>


    <LinearLayout
        android:id="@+id/bottomView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="@color/greenColor"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Fixed Bottombar"
            android:textColor="#ff00"
            android:textStyle="bold" />
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

我不想在LinearLayout内使用嵌套的ConstraintLayout

在以上布局中,我的NestedScrollView也没有滚动

我想使用ConstraintLayout

创建这种类型的屏幕

我的预期输出

enter image description here

我尝试设置Guideline,但对我来说无效,因为我不知道如何在上述布局中使用Guideline

有人可以只使用ConstraintLayout来帮助我创建此屏幕吗

如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。

2 个答案:

答案 0 :(得分:1)

因此,在查看了您的xml文件之后,我想出了以下解决方案来摆脱嵌套的LinearLayout层次结构:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="#000000"
    android:fitsSystemWindows="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

<androidx.core.widget.NestedScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:fillViewport="true"
    app:layout_constraintBottom_toTopOf="@id/tv_bottom_view"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <VideoView
            android:id="@+id/myVideoView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="visible"
            app:layout_constrainedHeight="true"
            app:layout_constraintBottom_toTopOf="@id/tv1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="0.85"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <TextSwitcher
            android:id="@+id/tvAnimText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:fontFamily="@font/knowledge_bold"
            android:textColor="@color/greenColor"
            android:textSize="@dimen/_100ssp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@id/myVideoView"
            app:layout_constraintEnd_toEndOf="@id/myVideoView"
            app:layout_constraintStart_toStartOf="@id/myVideoView"
            app:layout_constraintTop_toTopOf="@id/myVideoView"
            tools:text="1" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="Scrollbale view below my videoView"
            android:textColor="#ff00"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@id/tv2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/myVideoView" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="Scrollbale view below my videoView"
            android:textColor="#ff00"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@id/tv3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv1" />

        <TextView
            android:id="@+id/tv3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="Scrollbale view below my videoView"
            android:textColor="#ff00"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv2" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

<TextView
    android:id="@+id/tv_bottom_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:gravity="center"
    android:text="Fixed Bottombar"
    android:textColor="#ff00"
    android:textStyle="bold"
    app:layout_constrainedHeight="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHeight_default="percent"
    app:layout_constraintHeight_percent="0.08"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

让我知道评论中是否有任何查询。对于固定的底部栏TextView,现在您可以将高度百分比更改为要动态更改其高度的任何值。

答案 1 :(得分:0)

您可以对底部视图使用以下代码,以删除嵌套的LinearLayout:

  <TextView
            android:id="@+id/bottomView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Fixed Bottombar"
            android:textColor="#ff00"
            android:background="#00ff00"
            android:padding="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:textStyle="bold" />