如何根据设备屏幕缩放UI

时间:2018-02-15 10:05:17

标签: android xml android-layout android-studio user-interface

我正在处理一个项目,我正在尝试制作登录屏幕的布局。 Layout Image

这是我的xml代码

<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"
tools:context="com.ngo.ravi.idi.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="30dp"
    android:background="@color/colorPrimaryDark">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="35sp"
        android:textColor="#FFF"
        android:text="@string/welcome"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/autour_one"
        android:text="@string/descrip"
        android:textColor="#fff"
        android:textSize="15sp"/>
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/learnMore"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textColor="#fff"
        android:background="@drawable/buttonstyle2"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="@string/email" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <EditText
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:hint="@string/password" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:layout_marginTop="20dp"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:background="@drawable/buttonstyle"/>

</LinearLayout>

我的显示器出现问题。我正在使用模拟器来检查这个应用程序,我在4“,4.7”,5“,5.2”,6“显示器中尝试了这个并且它在5”及以上工作正常但是当我尝试使用4“和4.7”时我发现了这个文本不会缩小。有没有办法让这个应用程序按照设备自动缩放。

先谢谢

3 个答案:

答案 0 :(得分:1)

一个提供新尺寸单位的Android SDK-sdp(可缩放dp)。此尺寸单位随屏幕尺寸缩放。它可以帮助Android开发人员支持多个屏幕。

越过这个库:

dependencies {
  implementation 'com.intuit.sdp:sdp-android:1.0.5'
  implementation 'com.intuit.ssp:ssp-android:1.0.5'
}

如果要使用高度,宽度,边距,填充等,请使用@ dimen / _20sdp。 (按照您的要求选择尺寸)。

如果要使用TextSize,请使用@ diment / _12ssp。 (按照您的要求选择尺寸)。

请在下面的示例代码中查看:

<ImageView
        android:id="@+id/img_companies"
        android:layout_width="@dimen/_20sdp"
        android:layout_height="@dimen/_20sdp"
        android:src="@drawable/ic_companies"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"/>

这样,您可以使用两侧的高度和宽度固定大小

更新 sdp用于平板电脑和移动设备中的相同UI。 ssp用于平板电脑和移动设备的相同TextSize。 您也可以在另一个示例中看到此链接:https://github.com/intuit/sdp

希望它对您有用

答案 1 :(得分:0)

您必须创建多个屏幕布局。

参考:

您应该创建布局布局,例如,支持所有屏幕。

res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge

答案 2 :(得分:0)

您可以在不同大小的文件夹中创建多个dimen文件,如下所示:

RES /值-sw400p

RES /值-sw600p

RES /值-sw800p

并在每个文件夹中创建dimen.xml。

然后在每个文件中使dimen项目名称相同,但您可以根据您的要求分配其值不同

示例:

sw400p / dimen.xml

 textview_margin=10dp 

sw600p / dimen.xml

 textview_margin=16dp 

sw800p / dimen.xml

 textview_margin=20dp