Android 碎片在选项卡布局/视图页面2中不正确匹配2
我尝试了几种不同的方法来实现这一点,但是我在ViewPager中为选项卡布局托管的片段无法正确显示,除非我在片段中奇怪地定位组件。这是我的片段选项卡xml,代码是Java的:Android 碎片在选项卡布局/视图页面2中不正确匹配2,android,android-tablayout,android-viewpager2,Android,Android Tablayout,Android Viewpager2,我尝试了几种不同的方法来实现这一点,但是我在ViewPager中为选项卡布局托管的片段无法正确显示,除非我在片段中奇怪地定位组件。这是我的片段选项卡xml,代码是Java的: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="37dp" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF" />
</LinearLayout>
<Button
android:id="@+id/btnPost"
android:layout_width="147dp"
android:layout_height="47dp"
android:layout_marginBottom="16dp"
android:text="Post"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
我一直在使用模拟器在Android Studio上进行测试,当我在硬件上进行测试时,UI都乱七八糟,但也像上面那样定位屏幕,理想情况下,我希望能够将整个视图用于recycler视图,而不必将其这样放置,因为如果我使用match_parent或整个屏幕,它可能会显示在模拟器上,但不会显示在硬件设备上。有什么想法吗?我也尝试了协调器布局和相对布局,但无法得到精确的结果,尽管可能做得不正确
谢谢大家
编辑:硬件设备上的图片,该图片出现问题,但在emulator上创建视图时的奇怪位置看起来很好:
我不确定到底是什么问题,因为您没有提供一个示例,说明这在您的手机上是什么样子的。但是,我可以在约束布局中看到,您没有将LinearLayout设置为从父级开始,这可能会导致问题。对这些限制要非常小心。我用硬件设备的屏幕截图更新了这个问题。我确实尝试过从父视图的开头开始,但在尝试将视图放在视图寻呼机中以使用其全屏而不是奇数位置时仍然存在一些问题。您可以将父视图的开头添加到保存视图寻呼机的线性布局中(仅出于安全考虑)。关于XML上的recyclerView,我强烈建议您将内容变得更具dinamic。设置宽度和高度以包裹内容或匹配父对象,并为所需的边添加边距。SwiperFreshLayout也是如此。这些原始值可能会导致一些问题。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="387dp"
android:layout_height="491dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="387dp"
android:layout_height="491dp"
android:layout_marginBottom="8dp">
</androidx.recyclerview.widget.RecyclerView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="96dp"
android:layout_height="103dp"
android:visibility="invisible"
android:layout_marginBottom="342dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/txtViewEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>