android - 如何将线性布局居中到其父级的中间?

标签 android android-layout

我正在编写我的第一个完整的 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/

相关文章:

android - Https 帖子在 Android 模拟器上工作但在设备上不工作

android - Activity 确实出现了

android - 同一适配器中的多个布局管理器

Android:将多个 .XML 和对象膨胀到一个 .XML

java - Android:以编程方式仅设置 TextView 的一个填充

android - 如何在 Android 中将 Fragments 添加到后台堆栈

android - 如何知道键盘(dis)是否出现在Android中?

android - 如何通过位图交换图层列表中的图像?

android - 如何在安卓平板电脑中隐藏底部系统栏

android - 如何将数据从一个 fragment 发送到android中的另一个 fragment ?