Android Studio相对布局和线性布局不适用于某些设备

时间:2019-06-22 17:58:11

标签: android android-studio android-linearlayout relativelayout

我正在尝试开发一个用于项目管理的简单应用程序。问题在于应用程序屏幕的xml布局。应用程序屏幕按比例放置在不同设备上的比例不是很好。由于空间不足,有些元素甚至隐藏在某些设备中。

我已经尝试过同时使用线性布局和相对布局。我一直将“ Match_parent”属性用于相对布局和线性布局父块的宽度和高度。但仍然在某些屏幕尺寸下,某些元素未显示,它们在显示区域下方。

  <RelativeLayout
android:id="@+id/layout1"
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="@color/white"
tools:context=".login"
android:paddingTop="20dp"
>

<ImageView
android:id="@+id/loginImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/login_logo"
android:layout_centerHorizontal="true"
/>
<TextView
    android:id="@+id/loginText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="LOGIN"
    android:textColor="@color/orange"
    android:textSize="50sp"
    android:layout_below="@id/loginImage"
    />

<EditText
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/logintextbackground"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/loginText"
    android:hint="Username"
    android:textColorHint="@color/lightOrange"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:textColor="@color/lightOrange"
    android:maxLength="15"
    />
<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/logintextbackground"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/username"
    android:hint="Password"
    android:textColorHint="@color/lightOrange"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:textColor="@color/lightOrange"
    android:inputType="textPassword"
    android:maxLength="16"
    />

<Button
    android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/login_button"
    android:layout_below="@id/password"
    android:layout_centerHorizontal="true"
    />

<TextView
    android:id="@+id/orText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="or"
    android:textSize="20dp"
    android:textColor="@color/orange"
    android:layout_below="@id/loginButton"
    android:layout_centerHorizontal="true"
    />

<Button
    android:id="@+id/signUpButtonLogin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/signup"
    android:layout_below="@id/loginButton"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    />

1 个答案:

答案 0 :(得分:0)

ScrollView作为RelativeLayout的父项,就可以了。请参考下面的代码:

<ScrollView 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:background="@color/white">

    <RelativeLayout
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:paddingTop="20dp"
        tools:context=".login">

        <ImageView
            android:id="@+id/loginImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/login_logo" />

        <TextView
            android:id="@+id/loginText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginImage"
            android:layout_centerHorizontal="true"
            android:text="LOGIN"
            android:textColor="@color/orange"
            android:textSize="50sp" />

        <EditText
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginText"
            android:layout_centerHorizontal="true"
            android:background="@drawable/logintextbackground"
            android:hint="Username"
            android:maxLength="15"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:textColor="@color/lightOrange"
            android:textColorHint="@color/lightOrange" />

        <EditText
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/username"
            android:layout_centerHorizontal="true"
            android:background="@drawable/logintextbackground"
            android:hint="Password"
            android:inputType="textPassword"
            android:maxLength="16"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:textColor="@color/lightOrange"
            android:textColorHint="@color/lightOrange" />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/password"
            android:layout_centerHorizontal="true"
            android:background="@drawable/login_button" />

        <TextView
            android:id="@+id/orText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginButton"
            android:layout_centerHorizontal="true"
            android:text="or"
            android:textColor="@color/orange"
            android:textSize="20dp" />

        <Button
            android:id="@+id/signUpButtonLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/loginButton"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:background="@drawable/signup" />
    </RelativeLayout>
</ScrollView>

对于将来的开发,我建议您使用ConstraintLayout而不是LinearLayoutRelativeLayout,因为它减少了视图的嵌套。