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...我试图手动删除这些,但是当我回到"设计"选项卡,再回到"文本"选项卡中,这些再生.现在,我有三个问题:
dimens.xml?ian*_*ake 37
您会注意到所有绝对值都在tools命名空间中 - 这意味着它们不会编译到您的应用程序中,也不会在工具中使用(在本例中为可视化编辑器).它们只是确保从"设计"切换到"文本"选项卡始终保持一致,底层文件保持稳定.
- 有没有办法告诉Android Studio不生成这些?
没有.
- 我应该手动将它们放在dimens.xml中吗?
这些仅对工具有用,因此不应添加到dimens.xml最终APK中包含的单独文件中.
- 这些绝对会导致其他设备出现布局问题吗?
不,它们仅供工具使用.
我不确定您的原始问题是否包含您的整个布局,因为它引用了一个 ID 为 的小部件@+id/activity,因此问题可能出在您的布局中的其他地方。
确保没有部件内的存在ConstraintLayout有一个layout_width或layout_height的match_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)
| 归档时间: |
|
| 查看次数: |
16112 次 |
| 最近记录: |