如何创建具有 2 个固定部分和 1 个可扩展部分的 ConstraintLayout?
How do I create a ConstraintLayout with 2 fixed parts and 1 expandable?
关于这个话题,我一直在兜圈子,把自己搞得一头雾水。
我想创建一个包含 3 个垂直堆叠的组件的布局。第一个应该在屏幕的顶部,并占据它需要的任何 space (这会随着点击其中的内容而增加和减少)。第三个应该在底部并且是固定大小(Google 广告)。中间的应该占掉剩下的space.
我目前有(包装在 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" />
我是想添加一些垂直权重,对吧?但我的尝试没有任何区别——第一位在屏幕上浮动,所以它上面有空白 space。有人可以给我一个快速的答案或指向一个解释如何实现这一目标的教程吗?我已阅读 Android 文档,但找不到包含 'filling the leftover space'.
的任何内容
要根据需要用三个视图填充屏幕,请执行以下操作:
顶视图: width="0dp"(填充屏幕宽度); height="wrap_content" (会根据需要让它grow/shrink)
中视图: width="0dp"; height = "0dp"(在 top/bottom 限制范围内填充 space。)
底视图: width="0dp"; height="wrap_content"(由于内容的性质,实际上是固定大小)
这是这些概念的演示。我改变了一些东西,例如将一个片段变成了一个通用的View
,但是概念是一样的。
这张图片的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" 作为名称前缀 space。 =14=]
关于这个话题,我一直在兜圈子,把自己搞得一头雾水。
我想创建一个包含 3 个垂直堆叠的组件的布局。第一个应该在屏幕的顶部,并占据它需要的任何 space (这会随着点击其中的内容而增加和减少)。第三个应该在底部并且是固定大小(Google 广告)。中间的应该占掉剩下的space.
我目前有(包装在 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" />
我是想添加一些垂直权重,对吧?但我的尝试没有任何区别——第一位在屏幕上浮动,所以它上面有空白 space。有人可以给我一个快速的答案或指向一个解释如何实现这一目标的教程吗?我已阅读 Android 文档,但找不到包含 'filling the leftover space'.
的任何内容要根据需要用三个视图填充屏幕,请执行以下操作:
顶视图: width="0dp"(填充屏幕宽度); height="wrap_content" (会根据需要让它grow/shrink)
中视图: width="0dp"; height = "0dp"(在 top/bottom 限制范围内填充 space。)
底视图: width="0dp"; height="wrap_content"(由于内容的性质,实际上是固定大小)
这是这些概念的演示。我改变了一些东西,例如将一个片段变成了一个通用的View
,但是概念是一样的。
这张图片的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" 作为名称前缀 space。 =14=]