phone 和模拟器上的布局与设计器完全不同
Layout on phone & emulator quite different from designer
我正在尝试一个简单的布局练习,但我正在努力让一个简单的布局发挥作用。
在设计器中是这样的:
但是当 运行 时,模拟器或真实 phone 上的输出是:
Xml 标记如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hceng.myapplication.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail:"
tools:layout_editor_absoluteY="52dp"
tools:layout_editor_absoluteX="18dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="96dp" />
<EditText
android:id="@+id/editText"
android:layout_width="266dp"
android:layout_height="36dp"
android:ems="10"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteX="66dp"
tools:layout_editor_absoluteY="41dp" />
</android.support.constraint.ConstraintLayout>
我是这个标记语言的新手,但我认为它似乎很简单,始终采用绝对定位。不管怎样,在设计器中看到的布局就是我想要的。为什么以上不起作用?
如果没有绑定到 ConstraintLayout
你可以试试这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hceng.myapplication.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email_tv"
android:text="E-mail:" />
<EditText
android:id="@+id/editText"
android:layout_width="266dp"
android:layout_height="36dp"
android:ems="10"
android:layout_toRightOf="@+id/email_tv"
android:inputType="textEmailAddress" />
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:gravity="center" />
</LinearLayout>
约束布局有一些限制,但我不确定颜色是否是其中之一。
第一个
您可以尝试将约束布局更改为协调器布局或相对布局。
如果失败
可能是你的AndroidManifest中的目标SDK版本太高,如果目标版本高于phone版本,它会恢复默认。所以你可以尝试放置一个较低的目标版本。
根据文档
When you drop a view into the Layout Editor, it stays where you leave it even if it has no constraints. However, this is only to make editing easier; if a view has no constraints when you run your layout on a device, it is drawn at position [0,0] (the top-left corner).
这就是为什么您在编辑器中看到布局正确但在模拟器上看不到的原因。
我正在尝试一个简单的布局练习,但我正在努力让一个简单的布局发挥作用。
在设计器中是这样的:
但是当 运行 时,模拟器或真实 phone 上的输出是:
Xml 标记如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hceng.myapplication.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-mail:"
tools:layout_editor_absoluteY="52dp"
tools:layout_editor_absoluteX="18dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="96dp" />
<EditText
android:id="@+id/editText"
android:layout_width="266dp"
android:layout_height="36dp"
android:ems="10"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteX="66dp"
tools:layout_editor_absoluteY="41dp" />
</android.support.constraint.ConstraintLayout>
我是这个标记语言的新手,但我认为它似乎很简单,始终采用绝对定位。不管怎样,在设计器中看到的布局就是我想要的。为什么以上不起作用?
如果没有绑定到 ConstraintLayout
你可以试试这个<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hceng.myapplication.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email_tv"
android:text="E-mail:" />
<EditText
android:id="@+id/editText"
android:layout_width="266dp"
android:layout_height="36dp"
android:ems="10"
android:layout_toRightOf="@+id/email_tv"
android:inputType="textEmailAddress" />
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:gravity="center" />
</LinearLayout>
约束布局有一些限制,但我不确定颜色是否是其中之一。
第一个
您可以尝试将约束布局更改为协调器布局或相对布局。
如果失败
可能是你的AndroidManifest中的目标SDK版本太高,如果目标版本高于phone版本,它会恢复默认。所以你可以尝试放置一个较低的目标版本。
根据文档
When you drop a view into the Layout Editor, it stays where you leave it even if it has no constraints. However, this is only to make editing easier; if a view has no constraints when you run your layout on a device, it is drawn at position [0,0] (the top-left corner).
这就是为什么您在编辑器中看到布局正确但在模拟器上看不到的原因。