我的搜索栏和文本视图超出屏幕限制

My seekbars and textviews go off-screen limits

所以,经过几天的尝试,我根本找不到解决这个问题的方法,我已经在一个问题中问过这个问题,但是正如那里有人对我说的那样,我没有很好地表达我的问题。

如何让我的简单应用在所有屏幕上看起来都很好,我有一些屏幕截图可以告诉你是什么困扰着我:

当我 运行 我的应用程序在屏幕上显示模拟器时:1440x2560:560dpi 5.5" 它看起来不错:

Success Screenshot

但是当我 运行 它在 480x800:hdpi 4" 上时它看起来像这样:

Failure Screenshot

或者,如果我设置 android:layout_below="@id/containerLayout",我的搜索栏和它们上方的文本视图就会下降,离开屏幕(我可以看到第一个搜索栏,其他搜索栏被推到 "container" 的可见部分下方)

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:background="@drawable/background"
    tools:context="com.myapp.MainActivity">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RelativeLayout
            android:id="@+id/logoLayout"
            android:layout_width="match_parent"
            android:layout_height="110dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp">

            <ImageView
                android:id="@+id/logo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/logo"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/containerLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/buttonsLayout"
            android:layout_below="@+id/logoLayout"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"
            android:background="@drawable/container">

            <LinearLayout
                android:id="@+id/layoutSaDataText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/tutorialImage"
                android:layout_alignParentTop="true"
                android:layout_margin="20dp"
                android:gravity="top"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/datatext1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:textColor="@color/whiteDark"
                    android:textSize="19sp"
                    android:textStyle="italic"/>

                <TextView
                    android:id="@+id/datatext3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:textColor="@color/whiteDark"
                    android:textSize="16sp"/>

                <TextView
                    android:id="@+id/datatext2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:textColor="@color/whiteDark"
                    android:textSize="16sp"/>

                <TextView
                    android:id="@+id/datatext4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:textColor="@color/whiteDark"
                    android:textSize="16sp"/>

                <TextView
                    android:id="@+id/datatext5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:textAllCaps="false"
                    android:textColor="@color/whiteDark"
                    android:textSize="16sp"/>

            </LinearLayout>

            <ImageView
                android:id="@+id/tutorialImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/logoLayout"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="70dp"
                android:layout_marginTop="25dp"
                app:srcCompat="@drawable/usagetutorial"
                tools:ignore="NotSibling"/>

            <ProgressBar
                android:id="@+id/loadingbar"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:indeterminate="true"
                android:indeterminateTint="@color/progressBar"
                android:indeterminateTintMode="src_atop"
                />

            <LinearLayout
                android:id="@+id/layoutSaSeekerima"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="25dp"
                android:layout_marginEnd="25dp"
                android:layout_marginStart="25dp"
                android:gravity="bottom"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/playerLvlSeekerText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="center"
                    android:text="data 1"
                    android:visibility="gone"/>

                <SeekBar
                    android:id="@+id/playerLvlSeeker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="7dp"
                    android:max="250"
                    android:visibility="gone"/>


                <TextView
                    android:id="@+id/townHallLvlSeekerText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="center"
                    android:text="data 2"
                    android:visibility="gone"/>

                <SeekBar
                    android:id="@+id/townHallLvlSeeker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="7dp"
                    android:max="11"
                    android:visibility="gone"/>

                <TextView
                    android:id="@+id/trophiesCountSeekerText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="center"
                    android:text="data 3"
                    android:visibility="gone"/>

                <SeekBar
                    android:id="@+id/trophiesCountSeeker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="7dp"
                    android:max="100"
                    android:visibility="gone"/>

                <TextView
                    android:id="@+id/warStarsWonSeekerText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:gravity="center"
                    android:text="data 4"
                    android:visibility="gone"/>

                <SeekBar
                    android:id="@+id/warStarsWonSeeker"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="7dp"
                    android:max="100"
                    android:visibility="gone"/>
            </LinearLayout>

            <RelativeLayout
                android:id="@+id/usernameInputLayout"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:layout_marginEnd="25dp"
                android:layout_marginStart="25dp">

                <TextView
                    android:id="@+id/playertagbelow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_centerInParent="false"
                    android:layout_centerVertical="false"
                    android:text="insert your data below"
                    android:textAllCaps="true"
                    android:textColor="@android:color/darker_gray"/>

                <EditText
                    android:id="@+id/usernameInput"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_centerInParent="false"
                    android:layout_centerVertical="false"
                    android:ems="10"
                    android:hint="82VPLR89"
                    android:inputType="textPersonName"/>

            </RelativeLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/buttonsLayout"
            android:layout_width="match_parent"
            android:layout_height="90dp"
            android:layout_alignParentBottom="true"
            >

            <Button
                android:id="@+id/button2"
                android:layout_width="200dp"
                android:layout_height="70dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="false"
                android:layout_marginBottom="15dp"

                android:backgroundTint="@android:color/holo_green_dark"
                android:elevation="0dp"
                android:text="proceed"
                android:textSize="19sp"/>

            <Button
                android:id="@+id/button1"
                android:layout_width="200dp"
                android:layout_height="70dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="false"
                android:layout_marginBottom="15dp"

                android:backgroundTint="@android:color/holo_green_dark"
                android:elevation="0dp"
                android:text="verify user"
                android:textSize="17sp"/>
        </RelativeLayout>

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

是否有解决方案可以让我的搜索栏和所有内容根据屏幕尺寸调整大小,或者唯一的解决方案是为每个屏幕(小、正常、大)制作单独的布局?

我花了好几天时间试图解决这个问题,我根本没有使用像素,一切都是 dp 和数学 content/parent.. 但问题仍然存在。

谢谢转发!抱歉,代码太长,英语不是最好 :)

您是否正确设置了布局文件夹,例如 layout、layout-small、layout 和 layout-large????'

举个例子Android XML Layout for all Devices (Small/Normal/Large/XLarge, etc)

删除您拥有的所有布局(约束布局除外)并将所有内容放置到位:

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

和适当的约束(+边距)

所以在尝试找到解决方案的日复一日之后,我偶然发现了它.. 但这是最好的学习方式 :D

解决方案是在线性布局中正确设置文本视图和搜索栏的布局权重

android:layout_weight="1"

并将线性布局重力置于中心

android:gravity="center"

我的对象(文本视图和搜索栏)不再离开屏幕,它们只是适当地调整大小以适应 Layout/screen