将按钮放置在约束布局的底部

mtd*_*vem 5 layout android constraints

我试图将两个按钮放置在约束布局的底部。在 Android Studio 的设计视图中,一切看起来都不错,但是当我在调试中运行应用程序时,按钮看起来像是与屏幕顶部的最后一个字段对齐。我已经尝试过指南和障碍,但无法让障碍在我的工作室版本(3.0.1)中工作。如果我尝试按下显示屏顶部的按钮,当我切换到横向模式时它们就会消失。有什么建议,或者我应该在这个问题上采用另一种类型的布局吗?谢谢

我的设计图

我的设计图

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/location_detail"
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:fitsSystemWindows="true">

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="10dp"
    app:layout_constraintHeight_min="?attr/actionBarSize"
    app:layout_constraintTop_toTopOf="parent"/>


<TextView
    android:id="@+id/location_name_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:labelFor="@+id/location_name"
    android:text="@string/location_name_title"
    app:layout_constraintBaseline_toBaselineOf="@+id/location_name"
    app:layout_constraintLeft_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/guideline"/>

<EditText
    android:id="@+id/location_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:ems="13"
    android:inputType="textPersonName"
    app:layout_constraintLeft_toRightOf="@+id/guideline2"
    app:layout_constraintTop_toBottomOf="@+id/guideline"/>

<TextView
    android:id="@+id/location_region_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/location_region_title"
    app:layout_constraintBaseline_toBaselineOf="@+id/location_region"
    app:layout_constraintLeft_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/location_name_title"/>

<TextView
    android:id="@+id/location_region"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="@string/location_detail_region"
    app:layout_constraintLeft_toRightOf="@+id/guideline2"
    app:layout_constraintTop_toBottomOf="@+id/location_name"/>

<TextView
    android:id="@+id/location_country_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/location_country_title"
    app:layout_constraintBaseline_toBaselineOf="@+id/location_country"
    app:layout_constraintLeft_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/location_region_title"/>

<TextView
    android:id="@+id/location_country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="@string/location_detail_country"
    app:layout_constraintLeft_toRightOf="@+id/guideline2"
    app:layout_constraintTop_toBottomOf="@+id/location_region"/>

<TextView
    android:id="@+id/location_latitude_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/location_detail_latitude"
    app:layout_constraintBaseline_toBaselineOf="@+id/location_latitude"
    app:layout_constraintLeft_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/location_country_title"/>

<TextView
    android:id="@+id/location_latitude"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="@string/location_latitude_title"
    app:layout_constraintLeft_toRightOf="@+id/guideline2"
    app:layout_constraintTop_toBottomOf="@+id/location_country"/>

<TextView
    android:id="@+id/location_longitude_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/location_longitude_title"
    app:layout_constraintBaseline_toBaselineOf="@+id/location_longitude"
    app:layout_constraintLeft_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/location_latitude_title"/>

<TextView
    android:id="@+id/location_longitude"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="@string/location_detail_longitude"
    app:layout_constraintLeft_toRightOf="@+id/guideline2"
    app:layout_constraintTop_toBottomOf="@+id/location_latitude"/>

<android.support.constraint.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.27"/>

<android.support.v7.widget.AppCompatButton
    android:id="@+id/delete_location_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:onClick="saveLocation"
    android:text="@string/location_delete_button"
    app:layout_constraintEnd_toStartOf="@+id/save_location_button"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent" />


<android.support.v7.widget.AppCompatButton
    android:id="@+id/save_location_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:text="@string/location_save_button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/delete_location_button"
    app:layout_constraintBottom_toBottomOf="parent" />


</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

问题的屏幕截图

问题的屏幕截图

有关此屏幕的附加信息。会短暂显示一条 Toast 消息。此外,如果您想编辑位置名称,应该会出现一个软键盘。

我正在运行 Android 5.1.1 的 Nexus 7 上进行测试,但在运行 8.1.0 的 Nexus 6P 上运行时,行为是相同的。

mtd*_*vem 1

问题解决了。我一直在使用 FragmentTransaction 替换方法在我的应用程序中处理多个片段,将每个新片段视图放入我的一个 content_frame 中。问题不在于上面提到的约束布局,而在于我正在使用的容器视图。问题容器是:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxx.xxxx.weather.WeatherActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/my_toolbar"/>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

用RelativeLayout替换ConstraintLayout(并删除layout_constraint内容)解决了这个问题。显然,另一个 ConstraintLayout 中的 ConstraintLayout 使情况变得混乱。最初,我没有考虑容器视图,因为我使用的其他片段似乎没有任何问题。

之前我曾尝试将 FrameLayout 高度更改为“match_parent”,但这会导致其他问题,例如其他片段根本不显示。

感谢所有看过此内容的人。