【问题标题】:How to set height of a view in percent using ConstraintLayout如何使用 ConstraintLayout 以百分比设置视图的高度
【发布时间】:2019-10-10 05:11:09
【问题描述】:

目前正在学习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>

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

在上面的布局中我的NestedScrollView 没有滚动

我想使用ConstraintLayout创建这种类型的屏幕

我的预期输出

我尝试设置Guideline,但它对我不起作用,因为我不知道如何在上面的布局中使用Guideline

谁能帮我只使用ConstraintLayout创建这个屏幕

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

【问题讨论】:

  • 不清楚您要存档的内容?您当前的设计与您的共享屏幕设计相似。
  • @AmitPrajapati 是的,我想创建该布局而不使用嵌套的 LinearLayout
  • 你的意思是你想要存档相同的结果,只有约束布局对吗?
  • @AmitPrajapati 对,请检查更新的问题
  • @Goku,我可以知道你为什么使用 NestedScrollView 吗?

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


【解决方案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>

如果 cmets 中有任何查询,请告诉我。对于您的固定底栏TextView,现在您可以将高度百分比更改为您想要动态更改其高度的任何值。

【讨论】:

  • 嘿感谢重播由于某种原因Scrollbale view below my videoView 不可见,NestedScrollView 也没有滚动
  • @Goku 请检查更新的 xml 代码,我已经对VideoView 进行了一些更改(添加了高度百分比)
  • 抱歉重播晚了,经过一些更改后它工作正常
【解决方案2】:

您可以将以下代码用于底部视图,以删除您的嵌套线性布局:

  <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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    相关资源
    最近更新 更多