【问题标题】:Second View Not Appearing When App is Run应用程序运行时没有出现第二个视图
【发布时间】:2021-12-13 15:55:15
【问题描述】:

我已经研究这个奇怪的错误大约一天了,它完全让我感到困惑。我在框架布局中添加了一个 ImageButton,当我查看布局预览时,那里的按钮,但是当我在手机上运行应用程序时,它不存在;但是,当我将手机侧向转动时,会出现未设置任何属性的按钮。我发布了布局 xml 文件以及一些附带的布局照片和一些目录。任何帮助将不胜感激,谢谢。

附:出于某种原因,堆栈溢出对图像的行为真的很奇怪,所以我试图复制评论或其他内容中的链接

片段不工作:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/textViewLastUpdate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Last Update: HH:MM DD/MM/YYYY"
            android:textColor="@color/black"
            android:textSize="12sp"
            android:textStyle="bold|italic"
            android:layout_margin="10dp"
            android:textAlignment="textStart"/>
        <ImageButton
            android:id="@+id/imageButtonRefresh"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/ic_reload"
            android:tint="#2196F3"
            android:layout_marginStart="340dp"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            tools:ignore="UseAppTint" />
    </FrameLayout>

Entire Code:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        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"
        android:orientation="vertical">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:outlineSpotShadowColor="@color/black">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/textViewLastUpdate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Last Update: HH:MM DD/MM/YYYY"
                        android:textColor="@color/black"
                        android:textSize="12sp"
                        android:textStyle="bold|italic"
                        android:layout_margin="10dp"
                        android:textAlignment="textStart"/>
                    <ImageButton
                        android:id="@+id/imageButtonRefresh"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:src="@drawable/ic_reload"
                        android:tint="#2196F3"
                        android:layout_marginStart="340dp"
                        android:layout_marginEnd="10dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        tools:ignore="UseAppTint" />
                </FrameLayout>
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="25dp">
                    <TextView
                        android:id="@+id/textViewUPS"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="UPS: XXX"
                        android:layout_marginStart="60dp"/>
                    <TextView
                        android:id="@+id/textViewMains"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Mains: XXX"
                        android:layout_marginStart="210dp"/>
                </FrameLayout>
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="25dp">
                    <TextView
                        android:id="@+id/textViewUPSTemp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="UPS Temp: XXX"
                        android:layout_marginStart="60dp"/>
                    <TextView
                        android:id="@+id/textViewSOP"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Power Source: XXX"
                        android:layout_marginStart="210dp"/>
                </FrameLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="#c0c0c0"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"/>
                <TextView
                    android:id="@+id/textViewAdvancedInfo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="More Info . . ."
                    android:textAlignment="center"
                    android:padding="10dp"
                    android:textColor="#2196F3" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#c0c0c0"
            android:layout_margin="10dp"/>
    </LinearLayout>
[Image of drawable folders][1]
[Expected layout][2]
[Resulting Layout][3]
[Resulting Layout Side View With Image Artifact][4]

【问题讨论】:

  • 你在这里使用了大量边距android:layout_marginStart="340dp",你可能需要在布局上做一些基础知识,仅仅因为它在你的模拟器上看起来很好并不意味着它适用于所有设备尺寸
  • @a_local_nobody 几次迭代后我也有同样的想法,并删除了与它共享 FrameLayout 的 textView 并将其放在卡片的中心。发生了同样的问题,它没有出现,当我侧身转动手机时,伪影就在那里。

标签: android xml android-layout


【解决方案1】:

浏览目录后,我在 layout-v23 中找到了一个替代布局文件。布局与我的主布局文件夹中的布局不匹配,但与我手机上显示的布局匹配。我将代码从我的主布局复制到 v23 替代布局,修复了错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    相关资源
    最近更新 更多