java - 如何防止按钮和其他对象重叠?

标签 java android xml android-layout

当我使用 Android 模拟器预览时,布局预览正常。但是当我在手机中安装该应用程序时, View 会重叠。这可能是因为移动屏幕空间不足,但我不知道。请帮帮我。还帮助我使这个应用程序适用于所有屏幕尺寸。提前致谢。

第一张图片是模拟器预览,第二张图片是移动预览

First image is emulator preview 2nd is mobile preview

这是 xml 代码:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout

        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"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:orientation="vertical"
        android:background="@drawable/newbackground"
        tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            app:srcCompat="@drawable/dicee_logo" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/dicee_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/dice1" />

        <ImageView
            android:id="@+id/dicee_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/dice2" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/roll_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="61dp"
            android:background="@color/colorPrimary"
            android:text="@string/button_text"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/roll_button"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/roll_button"
            android:layout_marginBottom="25dp"
            android:background="@android:color/holo_blue_light"
            android:text="Next page" />
    </RelativeLayout>
</LinearLayout>

最佳答案

这将使 2 个 TextView 保持在底部,并将在您使用的任何 View 中展开

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleType="centerInside"
        android:src="@drawable/ic_word" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/dicee_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_word" />

        <ImageView
            android:id="@+id/dicee_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_pdf" />

    </LinearLayout>


    <Button
        android:id="@+id/roll_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:background="@color/colorPrimary"
        android:text="text"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:background="@android:color/holo_blue_light"
        android:text="Next page" />


</LinearLayout>

关于java - 如何防止按钮和其他对象重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48733617/

相关文章:

java - 如何为特定元素附加 XStream 转换器?

java - 将重要的应用内容保存到 xml 资源中

java - XML DOM setNodeValue ("") 在文本节点上,还会删除其包含元素的开始标记

xml - 如何在 XSD 架构中定义备用 XML 结构?

java - 向秒表类添加暂停和恢复方法

java - 通过 Java 为日历条目创建 Lotus Notes 插件

java - Netty - 传输带换行符的字符串的最佳方式?

机器人 : generic recyclerview adapter with data binding

android - 崩溃初始化 std::String

Android Studio - 无法下载 Artifact