1.什么是屏幕适配
当今使用android系统的屏幕尺寸这么多,为了让我们开发的程序能够比较美观的显示在不同屏幕的设备上,就必须做到屏幕适配。
2.尺寸适配怎么做
屏幕适配的主要解决方法是创建文件夹,例如一个1280x720P分辨率的手机要适配,就在res文件夹下创建一个values-1280x720分辨率的文件夹,这时文件夹从Project找,在文件夹上导航栏
从Android点击下拉选择Project。结果如图:
然后是各种分辨率下的属性值不同,这时在values-1280x720文件夹下新建一个xxx.xml,再在values-1184*720文件夹下新建一个xxx.xml(同名)再在xml文件里写上相同的属性,但是变量不同
代码如下
<resources>
<string name="text">one</string>
</resources>
<resources>
<string name="text">two</string>
</resources>
Activity的布局中调用
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/text"
/>
这样在1280x720和1148x720中的显示就分别是one和two,但是如果没有这种分辨率的会往下找,如果下面没有就会直接弹出,所以一般在values下设置一个默认值。
3.图片适配怎么做
图片适配的文件夹是自动生成的,同样从Project中打开,根据dpi(像素密度)决定,给出dpi表
x代表像素;
一般图像适配就是用同一张图片,修改他的分辨率,就是像素x像素;dpi越高,x像素越大;
4.什么是9.png图片
就是一张可以无限拉伸的图片
先给出一张9.png图片在android中打开的样子的图片
首先上面的点左边的点交叉的即为拉伸的坐标,右边和下面的黑线决定文字的显示范围
5.文字国际化(文字适配)怎么做
和尺寸适配差不多,如果想在英文系统显示英文,同样在res下新建一个values-en,把values中想要的xml文件直接复制到文件夹下,把中文的提示换成英文就行
6.横竖屏适配怎么做
这是需要在res文件夹下新建一个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=".Main2Activity">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@mipmap/chat_left_view_blue_bg"/>
//代替视频播放器
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="加入会员"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="立即加入可以获得无需广告"/>
</LinearLayout>
layout-land的xml文件代码
<?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=".Main2Activity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/chat_left_view_blue_bg"/>
</LinearLayout>