首页 > 解决方案 > Pixel 和 Nexus 手机上的 Android 布局混乱

问题描述

我在下面的图片中出现了这种奇怪的行为,只有在任何谷歌手机上,不管是旧的还是新的,都没关系,它只是错误地出现。可悲的是,它在其他所有手机上都按预期显示。恼人的是它在设计中正确地出现在图像中,但每个人都知道设计永远不会在 xml 中反映现实,另一个恼人的是它是如此简单,根是 CoordinatorLayout 而文本只是在根 :/

这是xml代码

<androidx.coordinatorlayout.widget.CoordinatorLayout(This layout is the )
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:touchListener="@{0L}"
    >
    .
    .
    .
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:background="@color/color_primary"/>

在此处输入图像描述

标签: androidxml

解决方案


由于这是一个约束布局,您应该放置start,endtop约束以获得正确的结果。

示例代码:

<androidx.coordinatorlayout.widget.CoordinatorLayout(This layout is the )
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:touchListener="@{0L}"
    >
    .
    .
    .
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Some Text"
        android:background="@color/color_primary"/>

像这样格式正确的东西对你有用。快乐编码!:)


推荐阅读