Android的屏幕适配

1、什么是屏幕适配?

基础概念

屏幕尺寸

屏幕尺寸是指屏幕对角线的长度,单位是英寸,1 inch=2.54 cm
屏幕分辨率

手机在横向和纵向上的像素点数总和,单位是像素(pixel),1px = 1像素点,举个栗子,1080x1920,即宽度方向上有1080个像素点,在高度方向上有1920个像素点。
屏幕像素密度

每英寸像素点个数,单位是dpi,dots per inch。为简便起见,Android 将所有屏幕密度分组为六种通用密度: 低、中、高、超高、超超高和超超超高。

ldpi(低)~120dpi
mdpi(中)~160dpi
hdpi(高)~240dpi
xhdpi(超高)~320dpi
xxhdpi(超超高)~480dpi
xxxhdpi(超超超高)~640dpi

2、尺寸适配怎么做?

首先,在Project→app→src→res建两个不同的value文件夹,名字后面包括你设置的尺寸大小,然后在其里面创建两个xml文件,里面代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="app_width">100dp</dimen>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="app_width">300dp</dimen>
</resources>

不同的尺寸里面的dp大小不同
写完之后,再在你要加的布局当中的fragment加上name这个属性,注意,属性名一定是要相同的,即可完成尺寸适配

3、图片适配怎么做?

图片有drawable-mdpi、drawable-ldpi、drawable-hdpi三个,这三个主要是为了支持多分辨率。

  drawable- hdpi、drawable- mdpi、drawable-ldpi的区别:

  (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)

  (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)

  (3)drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320)
  系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。
  在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。
  为了简化设计并且兼容更多的手机屏幕,平台依照尺寸和分辨率对屏幕进行了区分:三种尺寸:大,中,小。三种精度:高(hdpi),中(mdpi)和低(ldpi)。程序可以为这三种尺寸的屏幕提供默认资源,如有需要,还可以为各种精度的屏幕提供资源。在运行时,系统会根据屏幕布局加载正确尺寸或者精度的图片。一般情况下,我们图片的上传是选择mdpi,这是分辨率最高的。总结起来也就是图片的适配

4、什么是9.png的图片?

用简单的话来说,就是可以无限拉升且不变形的图片,这是我个人的理解。下面是对其进行分析:
这里写图片描述
图中黑点的意思:当你对该9.png的图片进行拉升时,只有这两个黑点的部位会拉升,其他的弧度,角度等等是不会改变的,也就是达成了图片可无限延伸且不会变形的效果;
图中黑线的意思:你的文字所显示的尺寸,只能是在这两根黑线的内部,超出时是看不见你的文字的;
这也就是对整个9.png图片性质的一个大体的分析

5、文字国际化(文字适配)怎么做?

在Project→app→src→res建一个名为values-en的文件夹,将values中的String.xml文件复制进去,代码如下:

<resources>
    <string name="app_name">My Application20180604</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
    <string name="btn_text">今天</string>
</resources>

这是在values文件夹中的String.xml代码中添加一个name,这个name是相对于你布局中的按钮的name

<resources>
    <string name="app_name">My Application20180604</string>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>
    <string name="btn_text">Today</string>
</resources>

这是在values-en文件夹中的String.xml代码中添加一个name,这个name与上面的相同,然后当你对语言切换是,它就会适配为你切换的语言。上述的是英语的切换,你也可以用相同的方法切换其他语言。

6、横竖屏适配怎么做?

与上面其他的适配创建文件位置一样,创建一个layout-land的文件夹,将你要横屏显示的布局写上去,再运行即可完成横竖屏的适配,代码段如下:

<?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:orientation="vertical"
    tools:context="com.example.fly.myapplication20180604.Main2Activity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/paopao" />
    </LinearLayout>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="加入会员,跳过广告" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="广告" />

</LinearLayout>

这是竖屏的布局代码

<?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:orientation="vertical"
    tools:context="com.example.fly.myapplication20180604.Main2Activity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/paopao" />
    </LinearLayout>


</LinearLayout>

这是横屏要显示的布局的代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值