首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android ConstraintLayout生成绝对值

Android ConstraintLayout生成绝对值
EN

Stack Overflow用户
提问于 2016-05-29 10:40:18
回答 3查看 20.5K关注 0票数 34

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

代码语言:javascript
复制
<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这样的绝对值...我试图手动删除它们,但当我返回到“设计”选项卡并返回到“文本”选项卡时,这些内容会重新生成。现在,我有三个问题:

  1. 有没有办法告诉Android Studio不要生成这些内容?
  2. 我应该手动将它们放在dimens.xml
  3. Would中吗?这些绝对内容会在其他设备上造成一些布局问题?
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-29 11:57:59

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

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

不是的。

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

这些只对工具有用,因此不应该添加到一个单独的dimens.xml文件中,该文件将包含在您的最终应用程序密钥中。

  1. 这些绝对设置是否会在其他设备上造成一些布局问题?

不,它们只由工具使用。

票数 42
EN

Stack Overflow用户

发布于 2017-07-19 02:10:02

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

确保ConstraintLayout中没有任何小部件的layout_widthlayout_heightmatch_parent

ConstraintLayout中包含的小部件不支持

MATCH_PARENT,但可以使用MATCH_CONSTRAINT定义类似的行为,并将相应的左/右或顶部/底部约束设置为"parent“。

Source

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

根据你发布的布局,在Android Studio替换之前,你的TextView可能有一个layout_widthlayout_heightmatch_parent

您应该将android:layout_width="match_parent"替换为

代码语言:javascript
复制
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"

和带有android:layout_height="match_parent"

代码语言:javascript
复制
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"

在您的特定布局中,您可能需要如下所示:

代码语言:javascript
复制
<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>
票数 5
EN

Stack Overflow用户

发布于 2020-04-20 03:23:01

作为给定答案的附注,您可以使用设计预览上方工具栏菜单中的魔棒图标。点击Infer Constraints按钮,这将自动在文本字段中添加一些行,并将工具转换为Constraint。

如下图所示:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37505641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档