Android ConstraintLayout生成绝对值

Ank*_*wal 28 android android-layout

我最近开始学习ConstraintLayoutAndroid Studio 2.2中的新功能,并注意到当我添加最简单的视图时,布局编辑器会自动生成一些绝对坐标.这是一个示例XML:

<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:id="@+id/activity_portfolio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.abc.Activity"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:text="@string/creator_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="246dp"
        tools:layout_editor_absoluteY="479dp"
        android:id="@+id/first_textview"
        app:layout_constraintRight_toRightOf="@+id/activity"
        android:layout_marginEnd="16dp"
        tools:layout_constraintRight_creator="0"
        app:layout_constraintBottom_toBottomOf="@+id/activity"
        android:layout_marginBottom="16dp"
        tools:layout_constraintBottom_creator="0" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

注意绝对喜欢81dp,246dp,479dp...我试图手动删除这些,但是当我回到"设计"选项卡,再回到"文本"选项卡中,这些再生.现在,我有三个问题:

  1. 有没有办法告诉Android Studio不生成这些?
  2. 我应该手动将它们放入dimens.xml
  3. 这些绝对会导致其他设备出现布局问题吗?

ian*_*ake 37

您会注意到所有绝对值都在tools命名空间中 - 这意味着它们不会编译到您的应用程序中,也不会在工具中使用(在本例中为可视化编辑器).它们只是确保从"设计"切换到"文本"选项卡始终保持一致,底层文件保持稳定.

  1. 有没有办法告诉Android Studio不生成这些?

没有.

  1. 我应该手动将它们放在dimens.xml中吗?

这些仅对工具有用,因此不应添加到dimens.xml最终APK中包含的单独文件中.

  1. 这些绝对会导致其他设备出现布局问题吗?

不,它们仅供工具使用.

  • 在我的情况下,IDE不断将布局layout_width从维度值更改为特定的DP.它很烦人. (3认同)
  • 他们真的很沮丧.需要禁用它们的方法 (3认同)

Jon*_* H. 5

我不确定您的原始问题是否包含您的整个布局,因为它引用了一个 ID 为 的小部件@+id/activity,因此问题可能出在您的布局中的其他地方。

确保没有部件内的存在ConstraintLayout有一个layout_widthlayout_heightmatch_parent

包含在 ConstraintLayout 中的小部件不支持 MATCH_PARENT,但可以通过使用 MATCH_CONSTRAINT 并将相应的左/右或上/下约束设置为“父”来定义类似的行为。

来源

如果使用match_parent,Android Studio 将生成这些绝对值,并替换match_parent为绝对尺寸。

根据您发布的布局,在 Android Studio 替换它之前,您TextView可能有一个layout_width或。layout_heightmatch_parent

你应该android:layout_width="match_parent"

android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"
Run Code Online (Sandbox Code Playgroud)

android:layout_height="match_parent"

android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"
Run Code Online (Sandbox Code Playgroud)

在您的特定布局中,您可能想要这样的东西:

<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:id="@+id/activity_portfolio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.abc.Activity">

    <TextView
        android:text="@string/creator_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/first_textview"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/activity"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)