我想实现以下目标。在这里输入图像描述
灰色部分是活动的背景,白色部分是一个水平的LinearLayout。我想在LinearLayout的顶部边框上创建微调器,但我所能做的就是把它放在布局内部或顶部。有什么方法可以完成这个任务吗,或者有什么变通的方法吗?
我们可以使用 ConstraintLayout
为此。我们需要使用两个属性的组合
layout_constraintTop_toBottomOf
layout_constraintBottom_toBottomOf
这将是中心的 Button
或其他 View
例子:
<?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">
<View
android:id="@+id/topView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_orange_dark"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:orientation="horizontal" />
<View
android:id="@+id/bottomView"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintTop_toBottomOf="@id/topView"
android:background="@android:color/holo_green_light"
android:orientation="horizontal" />
<com.google.android.material.button.MaterialButton
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/topView" // this
app:layout_constraintBottom_toBottomOf="@id/topView" // and this is important, it aligns the view to the top view
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
结果: