android - 无法使包含的 View 与布局中的父底部对齐

标签 android android-layout android-relativelayout

我正在尝试在 android 中做一个简单的布局,其中包含另一个文件,该文件是由三个按钮组成的底部导航。

我无法将包含的文件排列在底部。这是我的布局 xml(TextView 显示与父底部对齐,但包含的文件没有):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/patientDOBBlock"
    tools:text="Test Navigation Text"
    android:layout_alignParentBottom="true"/>
<include
    android:id="@+id/navigation"
    android:layout_below="@+id/patientDOBBlock"
    layout="@layout/bottom_navigation"
    android:layout_height="66dp"
    android:layout_width="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

包含的文件:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<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/task_base_buttons"
android:layout_width="match_parent"
android:layout_height="66dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/btnTaskBack"
        style="@style/NeutrifProText.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="@string/action_back"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

    <Button
        android:id="@+id/btnTaskContinue"
        style="@style/NeutrifProText.Button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight="0.6"
        android:background="@drawable/rounded_rect_color_primary"
        android:text="@string/action_continue"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnTaskSkip"
        app:layout_constraintStart_toEndOf="@+id/btnTaskBack" />

    <Button
        android:id="@+id/btnTaskSkip"
        style="@style/NeutrifProText.Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="@string/action_skip"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

 </android.support.constraint.ConstraintLayout>
 </merge>

知道为什么吗?顺便说一句,这个相同的包含文件显示在 fragment 的底部。

编辑:我包括了 fragment 的布局,其中布局确实出现在底部

<android.support.constraint.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:id="@+id/task_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.rangerhealth.rangerprovider.view.fragment.BaseTaskFragment"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:id="@+id/task_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="50dp"
    android:gravity="center"
    android:textColor="@color/black"
    android:textSize="@dimen/title_font_size"
    tools:text="Title"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/task_subtitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/task_title"
    android:layout_centerHorizontal="true"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="17dp"
    android:gravity="center"
    tools:text="Subtitle"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/task_title" />

<TextView
    android:id="@+id/task_description"
    android:layout_width="match_parent"
    android:layout_height="21dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    tools:text="Description"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/task_subtitle" />


<include layout="@layout/bottom_navigation"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    />

<RelativeLayout
    android:id="@+id/task_placeholder"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/task_description"
    app:layout_constraintBottom_toTopOf="@id/task_base_buttons"
    >

</RelativeLayout>
</android.support.constraint.ConstraintLayout>

最佳答案

在布局中,您将包含的布局与父底部对齐并位于 patentDOBBlock 下方。这两个指令是冲突的。我假设您希望底部导航和 TextView 都与父底部对齐并重叠。

在包含的布局中删除以下行:

android:layout_below="@+id/patientDOBBlock"

剩下以下布局:

<RelativeLayout 
    android:id="@+id/activity_background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/patientDOBBlock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        tools:text="Test Navigation Text" />

    <include
        android:id="@+id/navigation"
        layout="@layout/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="66dp"
        android:layout_alignParentBottom="true"
        app:layout_constraintBottom_toBottomOf="parent" />
</RelativeLayout>

在bottom_navigation.xml 中,删除merge 标记。来自 documentation :

The tag helps eliminate redundant view groups in your view hierarchy when including one layout within another.

由于您需要将 ConstraintLayout 保留在包含的文件中,因此您没有多余的 View 组,因此不需要合并标记。使用合并标签还可以防止将布局参数应用于包含文件。请参阅this .

You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag.

<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/task_base_buttons"
    android:layout_width="match_parent"
    android:layout_height="66dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <Button
        android:id="@+id/btnTaskBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="action_back"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

    <Button
        android:id="@+id/btnTaskContinue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_weight="0.6"
        android:background="@color/colorPrimary"
        android:text="action_continue"
        android:textColor="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnTaskSkip"
        app:layout_constraintStart_toEndOf="@+id/btnTaskBack" />

    <Button
        android:id="@+id/btnTaskSkip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_weight="0.2"
        android:background="@android:color/transparent"
        android:text="action_skip"
        android:textColor="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="@+id/btnTaskContinue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnTaskContinue" />

</android.support.constraint.ConstraintLayout>

结果如下:

enter image description here

尽管这不会造成任何损害,但 ConstraintLayout 中指定的约束将不会产生任何效果,除非父级也是 ConstraintLayout,因此可以删除它们。

关于android - 无法使包含的 View 与布局中的父底部对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47518177/

相关文章:

java - 如何从浏览器访问安卓

android - onActivityResult() 未被调用,但在其他地方有效

java - 连接丢失后 Firebase Firestore 需要很长时间才能重新连接

Android:LinearLayout$LayoutParams.<init> 上的 java.lang.NoSuchMethodError

Android textview 阴影不能使用 style.xml

android - 我可以在 android 中扩展自定义 View 吗?

android-layout - 如何知道 View 何时实际显示

android - 未使用 alignParentBottom

Android:使用 FrameLayout 和relativeLayout 的屏幕方向

android - 设置可见性后查看布局