简谈屏幕适配

简谈屏幕适配

什么是屏幕适配

Android中屏幕适配就是通过对尺寸单位、图片、文字、布局这四种类型的资源进行合理的设计和规划,在布局时合理利用各种类型的资源,让布局拥有适应能力,能在各种设备下保持良好的展现效果。

尺寸适配怎么做

创建文件夹,名字格式为 values-分辨率
这里写图片描述
文件夹下创建diments.xml文件,内容编辑:
这里举例,为960x540分辨率:

<dimen name="app_width">100dp</dimen>

1184x720分辨率:

<dimen name="app_width">300dp</dimen>

上面name可以任意取

在Activity布局文件中,例如一个按钮中:

android:layout_width="@dimen/app_width"

在layout_width调用dimens中的app_width

系统会根据你设备的分辨率向下寻找最接近的分辨率,然后取其中的app_width数值
如果设备的分辨率是最低的,那么运行时会报错,解决的方法是在values文件夹下创建一个默认的dimens

图片适配怎么做

在project下的mipmap中添加名字相同的图片:
这里写图片描述
这里写图片描述
然后再布局文件中添加该图片,系统会根据运行设备选择相应的文件夹

什么是9.png图片

.9.PNG是安卓开发里面的一种特殊的图片,这种格式的图片通过ADT自带的编辑工具生成,使用九宫格切分的方法,使图片支持在android 环境下的自适应展示。简而言之就是可以无限拉伸,且图片不会变形

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

国际化时,英语环境下的,文件夹命名为: values-en

国际化时,中文环境下的,文件夹命名为: values-zh

扫描二维码关注公众号,回复: 1697668 查看本文章

自己做的布局文件

<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" >
    <TextView 
        android:text="@string/taobao"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
<TextView 
        android:text="@string/erweima"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
<TextView 
        android:text="@string/sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        />
</LinearLayout>

在res文件夹下创建values-zh文件夹 然后创建stirng.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Demo_0103_文字国际化</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

    <string name="taobao">淘宝!</string>

    <string name="sex">性别!</string>

    <string name="erweima">二维码!</string>


</resources>

这样在你手机语言为中文时.会显示中文文字。

在res文件夹下创建values-en文件夹
然后创建stirng.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Demo_0103_文字国际化</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

    <string name="taobao">Tao Bao!</string>

    <string name="sex">Sex!</string>

    <string name="erweima">QR code!</string>


</resources>

这样在你手机语言为英文时.会显示英文文字。

横竖屏适配怎么做

在res目录下建立layout-land目录,相应的layout文件名不变,比如main.xml。layout-land是横屏的layout,其他的不用管,横竖屏切换时程序为调用Activity的onCreate方法,从而加载相应的布局。

猜你喜欢

转载自blog.csdn.net/source_sc/article/details/80610772