【问题标题】:Horizontal Layout messed up when using Jetpack Fragment Navigation使用 Jetpack 片段导航时水平布局混乱
【发布时间】:2020-11-06 16:57:10
【问题描述】:

Android 开发初学者在这里。我遵循了基于片段导航的教程。更改设备方向时遇到麻烦。它在真实设备上运行良好,但在模拟器中它是代理。

我已经阅读了一些关于在设备方向更改时重新渲染视图的内容,但在我的情况下,整个布局都搞砸了,包括顶栏和底栏(背面等)。截图和代码如下。

注意:在 XML 布局文件的设计选项卡中看到布局看起来完全没有问题。

这是垂直方向的样子:

主要活动和主要片段的代码如下:

<?xml version="1.0" encoding="utf-8"?>
    <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"
    tools:context=".MainActivity">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的主要片段是这个:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    tools:context=".MainFragment"
    android:id="@+id/parentLayout">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="30dp"
        android:gravity="center">

        ...some stuff here...

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 这有点猜测,但ConstraintLayout 不支持 match_parent。我在您的第二个代码片段中看到您的孩子 LinearLayout 正在使用它;尝试将其更改为带有约束的0dp(例如在您的第一个片段中)。我以前见过match_parent 造成非常奇怪的事情。
  • 不要认为这是应用程序的问题。我认为状态栏不会因为应用程序而被渲染两次。当您在模拟器上更改方向时,其他应用程序是否正常工作??
  • @BenP。我尝试使用它,但没有任何改变。
  • @KyzerSoze 你是对的!其他应用程序也搞砸了!也许是我的模拟器……
  • 我不能说是什么导致了这个问题。首先,我建议您检查您是否有适当的系统要求,然后查看硬件加速。 Reference doc。在 SDK 管理器中,安装或更新 SDK 工具、Android Emulator、Hypervisor Driver/Emulator Accelerator(以适用者为准)

标签: android xml android-layout kotlin


【解决方案1】:

=> 这看起来不正确:

android:layout_width="0dp"
android:layout_height="0dp"

尝试按照这个例子:

<?xml version="1.0" encoding="utf-8"?>
    <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"
    tools:context=".MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 2014-01-31
    • 2018-07-02
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多