Java 在Android中使用ConstraintLayout时,UI为空
我在片段中使用ConstraintLayout,但是当我运行应用程序时,UI是空的,下面是片段和显示片段的活动 片段布局Java 在Android中使用ConstraintLayout时,UI为空,java,android,android-fragments,kotlin,android-constraintlayout,Java,Android,Android Fragments,Kotlin,Android Constraintlayout,我在片段中使用ConstraintLayout,但是当我运行应用程序时,UI是空的,下面是片段和显示片段的活动 片段布局 主要活动 包含NavHostFragment <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android
主要活动
包含NavHostFragment
<androidx.constraintlayout.widget.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"
android:background="@null"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="395dp"
android:layout_height="340dp" app:navGraph="@navigation/nav_graph" app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/fragment"/>
</androidx.constraintlayout.widget.ConstraintLayout>
不同的手机有不同的屏幕大小,在您的布局中,您在视图上使用固定大小(例如,固定大小为50dp),结果是在一个屏幕(android studio预览屏幕)上看起来不错的东西在另一个屏幕(您的实际手机)上看起来不好 您可能希望执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".FirstFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/firstFragButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Go" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first_fragment"
app:layout_constraintBottom_toTopOf="@+id/firstFragButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
NavHostFragment也一样,可以使用:
android:layout\u width=“0dp”
和
android:layout\u height=“0dp”
使您的视图遍布整个屏幕,或使用类似的方法使您的视图具有响应性:
app:layout\u constraintWidth\u percent=“0.8”
app:layout\u constraintHeight\u percent=“0.5”
这将告诉您的视图宽度为屏幕大小的80%,高度为50%:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".FirstFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="@+id/firstFragButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Go" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first_fragment"
app:layout_constraintBottom_toTopOf="@+id/firstFragButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
您可以查看我的示例,因为图像上的白色背景可能不太清晰
一般来说,如果您使用的是ConstraintLayout,我建议您将其与和一起使用,以支持不同的屏幕大小。在按钮中,您的意思是:android:layout_marginBottom=“384dp”?我只是使用设计视图将它们拖到这些位置,它在framelayout上运行良好,但在ConstraintLayout@FiveElements中不起作用。我认为最好避免在设计视图中拖动视图,或者最好完全避免ConstantLayout,God。我认为您只是误解了ConstantLayout,请阅读文档,我相信您会发现它很容易使用