约束不以 phone 为中心的视图
Constraints not centering views on phone
我有一个奇怪的错误。我真的不知道为什么。
因此,在编辑器上,我的约束按我的意愿工作。但在 phone 上,他们没有。
图片(我没有足够的声誉来显示图片..):
android studio
phone problem
代码:
<?xml version="1.0" encoding="utf-8"?>
<layout
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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".fragments.home.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
提前致谢!
您是否尝试过将 ConstraintLayout 高度设置为 "match_parent"?
似乎您将其限制为 TextView 的组合高度。
另外,你不应该这样设置BottomToTop和TopToBottom,应该依赖父级。您可以通过在编辑器中选择两个视图,右键单击选择 Chain Vertically
.
来轻松完成此操作
没关系,刚刚发现问题。
activity_main.xml
中的 NavHostFragment
上的宽度和高度都需要 match_parent
。
<!-- navigation fragment -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"
android:layout_height="match_parent"
android:layout_width="match_parent" />
我有一个奇怪的错误。我真的不知道为什么。
因此,在编辑器上,我的约束按我的意愿工作。但在 phone 上,他们没有。
图片(我没有足够的声誉来显示图片..):
android studio
phone problem
代码:
<?xml version="1.0" encoding="utf-8"?>
<layout
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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".fragments.home.HomeFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
提前致谢!
您是否尝试过将 ConstraintLayout 高度设置为 "match_parent"? 似乎您将其限制为 TextView 的组合高度。
另外,你不应该这样设置BottomToTop和TopToBottom,应该依赖父级。您可以通过在编辑器中选择两个视图,右键单击选择 Chain Vertically
.
没关系,刚刚发现问题。
activity_main.xml
中的 NavHostFragment
上的宽度和高度都需要 match_parent
。
<!-- navigation fragment -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"
android:layout_height="match_parent"
android:layout_width="match_parent" />