【问题标题】:Android Linear Layout keeps reordering children whenever I change it's properties每当我更改其属性时,Android 线性布局都会不断重新排序子级
【发布时间】:2020-10-27 12:48:03
【问题描述】:

我是一个android初学者,如果这是一个愚蠢的问题,请原谅我!

我正在尝试创建一个包含表单的基本活动 - 一系列问题 (TextViews),每个问题后跟一个答案字段 (EditTexts),底部有一个 SUBMIT 按钮。表单比屏幕长一点,所以我将它们全部包含在一个 ScrollView 中,然后包含在一个线性布局中。

但每次我更新线性布局或滚动视图的属性(例如修改容器的大小,或设置中心重心)时,我的线性布局中的项目都会重新排序,以便提交按钮位于顶部,然后是所有的 EditText,然后是所有的 TextView。 XML 也被重新排序。

它并没有阻止我的应用程序运行,因为我可以重新排序它们然后按 go(只要我不需要调整任何东西)但是真的很难使用,因为我花了很长时间重新排序它们 - 我想知道我做错了什么?

任何帮助表示赞赏!

xml如下(还没有附带的java,activity还是空白):

<?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=".CreateNewEventActivity">

    <ScrollView
        android:layout_width="409dp"
        android:layout_height="601dp"
        android:layout_marginBottom="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="CREATE" />

            <EditText
                android:id="@+id/dateEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="date"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/locationEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/themeEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/participantEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:textSize="28sp" />

            <EditText
                android:id="@+id/chooseEventIdEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/introTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center|center_horizontal|center_vertical"
                android:text="INTRO \n"
                android:textAlignment="center"
                android:textSize="40sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dateTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/locationTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/themeTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/participantTextView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/participantTextView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

            <TextView
                android:id="@+id/choseEventIdTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textSize="28sp" />

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

desired order reordered after amending the size of the LinerLayout

【问题讨论】:

  • 在您重新格式化代码 (Ctrl+alt+L) 后是否对视图重新排序?
  • 是的...(我什至不知道这是一个功能)

标签: android xml android-layout android-linearlayout android-scrollview


【解决方案1】:

这是 Android Studio 3.5 Canary 8 中 android studio 的一个已知问题,已在 Android studio version 3.5.2 中修复(请查看 issue #129457736 了解更多详情)。因此,您需要做的就是更新您的 Android Studio 版本

编辑 XML 代码时,IDE 可能会应用不正确的代码样式 当您从菜单栏中选择代码 > 重新格式化代码时。要解决这个问题 问题,重置相应的Android代码样式如下。

如果您想保留 Android Studio 版本,请执行以下操作:

  1. 通过单击文件 > 设置(在 macOS 上,Android Studio > 首选项)打开“设置”窗口。
  2. 在左侧面板中,单击编辑器 > 代码样式 > XML。
  3. 在右侧面板右上角附近,选择 Set from > Predefined Style > Android
  4. 点击确定(可能需要重启 Android Studio)

【讨论】:

  • hmmmm,我的 Android Studio 告诉我它是 4.0 版,于 2020 年 5 月 20 日发布?我应该把它恢复到 3.5.2 吗?
  • 是最新版本吗?您应该更新到最新版本。如果不起作用,请尝试按照上述步骤进行修复
猜你喜欢
  • 2021-05-14
  • 1970-01-01
  • 2017-04-06
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
相关资源
最近更新 更多