我正在编写我的第一个完整的 Android 应用程序,我想将线性布局居中,以便布局本身居中,而不仅仅是布局内部的内容。我在旧帖子中看到 android:layout_gravity
用于执行此操作,但当我将其输入 Activity 的 XML 时,there are no suggestions , 和 nothing happens when fully entered .
难道我不应该使用线性布局来实现这一目标吗?我是否应该使其大小 match_parent
并仅限制其所有子项的大小?我对布局的想法是限制线性布局的大小,将其居中,并使其所有子级的水平大小 match_parent
。
这是我的 Activity 的 XML 以供引用:
<?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.frc5113.combustiblescouting.MainActivity">
<LinearLayout
android:layout_width="140dp"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Team Number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Color" />
</LinearLayout>
最佳答案
您的根布局是 ConstraintLayout
。我认为它不支持 layout_gravity
属性。您应该使用 LinearLayout
或使用与 Root View 相关的约束,如 here 所述.
关于android - 如何将线性布局居中到其父级的中间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49576667/