我最近开始学习Android Studio2.2中的新ConstraintLayout
,并注意到当我添加最简单的视图时,布局编辑器会自动生成一些绝对坐标。下面是一个示例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>
注意像81dp
,246dp
,479dp
这样的绝对值...我试图手动删除它们,但当我返回到“设计”选项卡并返回到“文本”选项卡时,这些内容会重新生成。现在,我有三个问题:
dimens.xml
发布于 2016-05-29 11:57:59
您将注意到,所有绝对值都在tools
名称空间中-这意味着它们不会编译到您的应用程序中,也不会在工具(在本例中是可视化编辑器)中使用。它们只是为了确保从“设计”到“文本”选项卡的切换总是一致的,并且底层文件保持稳定。
不是的。
这些只对工具有用,因此不应该添加到一个单独的dimens.xml
文件中,该文件将包含在您的最终应用程序密钥中。
不,它们只由工具使用。
发布于 2017-07-19 02:10:02
我不确定您的原始问题是否包含整个布局,因为它引用了一个id为@+id/activity
的小部件,所以问题可能出在布局的其他地方。
确保ConstraintLayout
中没有任何小部件的layout_width
或layout_height
为match_parent
。
ConstraintLayout中包含的小部件不支持
MATCH_PARENT,但可以使用MATCH_CONSTRAINT定义类似的行为,并将相应的左/右或顶部/底部约束设置为"parent“。
如果您使用match_parent
,Android Studio将生成这些绝对值,并将match_parent
替换为绝对维度。
根据你发布的布局,在Android Studio替换之前,你的TextView
可能有一个layout_width
或layout_height
为match_parent
。
您应该将android:layout_width="match_parent"
替换为
android:layout_width="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndtOf="parent"
和带有android:layout_height="match_parent"
的
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomtOf="parent"
在您的特定布局中,您可能需要如下所示:
<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>
发布于 2020-04-20 03:23:01
作为给定答案的附注,您可以使用设计预览上方工具栏菜单中的魔棒图标。点击Infer Constraints按钮,这将自动在文本字段中添加一些行,并将工具转换为Constraint。
如下图所示:
https://stackoverflow.com/questions/37505641
复制相似问题