图片适配和布局适配

1_图片适配简介
图片适配是所有的手机屏幕都切一套图片吗?是每一种屏幕都切一套图片吗?
实际工作中图片一般是切两套主流屏幕的图片,不多于三套。图片越多会导致应用程序变大,不利于下载和推广。
两个屏幕如下:
480*800
1280*720


2_创建演示工程ScreenAdapter_pic


布局文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/mm" />


</RelativeLayout>




在drawable,drawable-ldip,drawable-mdip,drawable-hdip,drawable-xdip ,drawable-xxdip
放置同名不但图片风格不一样的图片mm.jpg




总结:在不同级别的drawable目录下放置不同的图片,但相同的名字时,运行在不同的手机和设备中,会根据手机的密度来找对应的图片


在drawable目录中放置图片,其他所有的没有放置,这个时候所有的显示就显示drawable-xhdpi目录下的图片

02_布局适配
适配方法有两种,指定目录
按照密度比来适配
1_创建新的工程ScreenApapter_layout
在res目录下创建layout-1280x720目录
拷贝布局修改文字描述
注意这个符号不是乘以符号,是a,b,c的x
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是aot1880x720的布局" />
</RelativeLayout>
运行在不同分辨率的模拟器中
在res目录下创建layout-320x240目录
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是layout-1280×720的布局" />
</RelativeLayout>
2_总结
布局适配:布局文件 放置在不同的layout-分辨率(大的写在前面)的目录下,对应的手机会加载对应的布局文件。

发布了13 篇原创文章 · 获赞 11 · 访问量 9251

猜你喜欢

转载自blog.csdn.net/wangzizhong201205/article/details/52987137