本文介绍了Android ConstraintLayout生成绝对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have recently started learning the new ConstraintLayout in Android Studio 2.2 and noticed that when I add simplest of the views, the layout editor automatically generates some absolute coordinates. Here is a sample 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>

注意诸如81dp246dp479dp之类的绝对值...我试图手动删除它们,但是当我回到设计"选项卡并回到文本"选项卡时,这些再生.现在,我有三个问题:

Notice the absolutes like 81dp, 246dp, 479dp... I tried to manually delete these, but when I go back to the "Design" tab and come back to the "Text" tab, these regenerate. Now, I have three questions:

  1. 有没有办法告诉Android Studio不生成这些?
  2. 我应该手动将它们放置在中吗?
  3. 这些绝对值会在其他设备中引起一些布局问题吗?

推荐答案

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

You'll note that all of the absolute values are in the tools namespace - this means they are not compiled into your app, nor used in anything but in the tools (and in this case, the visual editor). They are simply to ensure that switching from the Design to Text tab is always consistent, with the underlying files remaining stable.

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

否.

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

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

These are only useful for the tools and therefore should not be added to a separate dimens.xml file that would be included in your final APK.

  1. 这些绝对值会在其他设备中引起一些布局问题吗?

否,它们仅由工具使用.

No, they are only used by the tools.

这篇关于Android ConstraintLayout生成绝对值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 00:44