【发布时间】:2018-01-02 15:21:47
【问题描述】:
我正在使用 android studio 创建我的第一个应用程序,这是我的第一个问题:
我想试试 ConstraintLayout。我已经使用设计编辑器(一起单击)使用 ConstraintLayout 构建了布局。当我在 Android Emulator 中尝试布局时,所有按钮都移到了左上角:( 除了我创建第一个项目时自动生成的“Hello World”标签。
Label 和 Button 的区别在于,缺少一些以 app:layout_constraint.... 开头的代码行。你可以在代码中看到它。
我做错了什么,还是一个错误? 我很高兴得到答案! :)
<?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="com.example.u0017007.coffeecounter.MainActivity">
<TextView
android:layout_width="136dp"
android:layout_height="30dp"
android:text="Hello World!"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.234" />
<Button
android:id="@+id/buttonAddCoffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="false"
android:text="@string/add_coffee"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="231dp" />
<Button
android:id="@+id/buttonRemoveCoffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remove_coffee"
tools:layout_editor_absoluteX="236dp"
tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>
【问题讨论】:
-
使用RelativeLayout而不是ConstraintLayout。
标签: android layout constraints android-constraintlayout