如何创建具有2个固定部分和1个可扩展部分的ConstraintLayout?

问题描述 投票:0回答:1

关于这个话题,我一直在圈子里走了一圈,让自己完全糊涂了。

我想创建一个有3个组件的布局,垂直堆叠。第一个应该在屏幕的顶部,并占用它需要的大量空间(这将随着其中的内容被点击而增加和减少)。第三个应位于底部,并且是固定大小(Google广告)。中间的那个应该占用剩下的空间。

我目前有(包裹在ConstraintLayout中):

    <android.support.constraint.ConstraintLayout
        android:id="@+id/cl_create_chart"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/fragment_charts_list"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
    ... contents of layout ...
   </ConstraintLayout>
   <fragment
        android:id="@+id/fragment_charts_list"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/cl_create_chart"
        app:layout_constraintBottom_toTopOf="@+id/au_adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout="@layout/fragment_charts_list" />
    <com.google.android.gms.ads.AdView
        android:id="@+id/au_adView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="..."
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintStart_toStartOf="parent" />

我打算添加一些垂直权重,对吗?但我尝试的任何东西都没有任何区别 - 第一位是浮在屏幕上,所以它上面有空白区域。有人可以给我一个快速的答案或指向我解释如何实现这一目标的教程?我已经阅读了Android文档,但找不到任何包含“填充剩余空间”的内容。

android-layout android-constraintlayout
1个回答
1
投票

要根据需要使用三个视图填充屏幕,请执行以下操作:

顶视图:width =“0dp”(填充屏幕宽度); height =“wrap_content”(将根据需要增长/缩小)

中间视图:width =“0dp”; height =“0dp”(填充顶部/底部约束内的空间。)

底视图:width =“0dp”; height =“wrap_content”(由于内容的性质,确实是一个固定的大小)

这是这些概念的演示。我改变了一些东西,例如,将片段制作成通用的View,但概念是相同的。

enter image description here

这张图片的XML:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cl_create_chart"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/fragment_charts_list"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/lorem_ipsum"
            android:textSize="20sp" />
    </android.support.constraint.ConstraintLayout>

    <View
        android:id="@+id/fragment_charts_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toTopOf="@+id/au_adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cl_create_chart" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/au_adView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

我还要指出,当您需要Adview中约束布局的“app”前缀时,您将“ads”作为命名空间前缀。

© www.soinside.com 2019 - 2024. All rights reserved.