Android 如何根据设备屏幕缩放UI
我正在做一个项目,我正试图使登录屏幕的布局 这是我的xml代码Android 如何根据设备屏幕缩放UI,android,xml,android-layout,android-studio,user-interface,Android,Xml,Android Layout,Android Studio,User Interface,我正在做一个项目,我正试图使登录屏幕的布局 这是我的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="v
<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>
我的显示器有问题。我正在使用emulator来检查这个应用程序,我在4英寸、4.7英寸、5英寸、5.2英寸、6英寸的屏幕上试用过,它在5英寸及以上的屏幕上运行良好,但当我在4英寸和4.7英寸的屏幕上试用时,我发现它的文本不会缩小。是否有任何方法可以使此应用程序根据设备自动缩放
提前感谢您必须创建多屏幕布局 参考:
res/layout-small
res/layout-normal
res/layout-large
res/layout-xlarge
您可以在不同大小的文件夹中创建多个dimen文件,如下所示: res/values-sw400p res/values-sw600p res/values-sw800p 并在每个文件夹中创建dimen.xml 然后使每个文件中的dimen项目名称相同,但您可以根据需要分配不同的值 例如: sw400p/dimen.xml
textview_margin=10dp
textview_margin=16dp
textview_margin=20dp
sw600p/dimen.xml
textview_margin=10dp
textview_margin=16dp
textview_margin=20dp
sw800p/dimen.xml
textview_margin=10dp
textview_margin=16dp
textview_margin=20dp
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用于平板电脑和移动设备的相同文本大小。
您还可以查看此链接以了解另一个示例:
希望对u有用参考:使用dimens..为不同的屏幕创建不同的大小