使用滚动视图和约束布局的奇怪输出
Weird output using the scrollView and Constraint layout
<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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar2"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:id="@+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="Customer Name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
app:layout_constraintBaseline_toBaselineOf="@+id/customer_name"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/customer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintLeft_toRightOf="@+id/textView5"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
请检查此图像的输出 。
两边的内容都在屏幕外。我不知道为什么?我什至指定了左右边距 16dp(8dp+8dp)。但我不认为这是成功的。
为什么它表现得很奇怪?是因为我在 android 版本 4.2.1 上测试吗?
或者有什么原因?
OS- 4.2.1
我已经在 5.1.1 上检查过它工作正常。
输出是这样的,因为:
没有在 ConstraintLayout
中指定 android:orientation
(默认为 "horizontal"
)
android:layout_width
值设置为"wrap_content"
三种解决方案供您选择:
将 ConstraintLayout
中的 android:orientation
更改为 "vertical"
并将 TextView
和 EditText
中的 android:layout_width
设置为match_parent
将你的两个View中的android:layout_width
设置为0dp
,然后使用android:weight
.
(最佳方案)删除TextView
,将EditText
的android:layout_width
设置为[=22] =].然后像这样在 EditText
上使用 android:hint
:
android:hint = "Customer Name"
<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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginRight="0dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/toolbar2"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:id="@+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_horizontal_margin">
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="Customer Name"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
app:layout_constraintBaseline_toBaselineOf="@+id/customer_name"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/customer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
app:layout_constraintLeft_toRightOf="@+id/textView5"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
请检查此图像的输出
两边的内容都在屏幕外。我不知道为什么?我什至指定了左右边距 16dp(8dp+8dp)。但我不认为这是成功的。 为什么它表现得很奇怪?是因为我在 android 版本 4.2.1 上测试吗? 或者有什么原因?
OS- 4.2.1
我已经在 5.1.1 上检查过它工作正常。
输出是这样的,因为:
没有在
ConstraintLayout
中指定android:orientation
(默认为"horizontal"
)android:layout_width
值设置为"wrap_content"
三种解决方案供您选择:
将
ConstraintLayout
中的android:orientation
更改为"vertical"
并将TextView
和EditText
中的android:layout_width
设置为match_parent
将你的两个View中的
android:layout_width
设置为0dp
,然后使用android:weight
.(最佳方案)删除
TextView
,将EditText
的android:layout_width
设置为[=22] =].然后像这样在EditText
上使用android:hint
:android:hint = "Customer Name"