适配问题(屏幕,图片,文字,横竖屏,尺寸)

什么是屏幕适配

使得某一元素在Android不同尺寸、不同分辨率的手机上具备相同的显示效果

名称 像素密度范围
mdip 120dpi
hdip 160dpi
xhdip 240dpi
xxhdip 320dpi
xxxhdip 480dip

尺寸适配怎么做

在res下面建不同的文件,例如要建940x600分辨率的,文件名就要叫values-940x600
这里写图片描述

再在values-940x600下建一个名字dimens.xml的文件
这里写图片描述
dimens.xml的文件内容如下:

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

如果还要建不同分辨率步骤和上面一样,只要把文件的数据改一下
例如要写values-1080x960

图片适配怎么做

在项目的res目录下,会多出几个以mipmap开头的文件夹。
这里写图片描述
相同的图片放在不同的文件下,不同的屏幕就会调用适合自己分辨率的图片
这里写图片描述

270*480像素的图片,属于xxhdpi文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/android_logo"
        />

</LinearLayout>

效果图:
这里写图片描述
移动到drawable-mdpi文件夹,重新运行程序
效果图:这里写图片描述

什么是9.png图片

-.9.png是一种可以自定义拉伸特定区域的图片格式。
- .9.png图片在图片拉伸的时候特定的区域不会发生图片失真;
- .9.png图片作为背景图的时候可以指定内容显示区域;
这里写图片描述
这里写图片描述
上边黑线:横向拉伸区域,必须要画的,拉伸是横向拉伸,如右边的区域第二个拉伸的就是横向拉伸的效果
左边黑线:纵向拉伸区域,必须要画的,拉伸是纵向拉伸,如右边的区域第一个拉伸的就是纵向拉伸的效果
下边黑线:可选,横向内容显示区域
右边黑线:可选,纵向内容显示区域

文字国际化怎么做

  1. 英语环境下的,文件夹命名为:values-en
  2. 美国英文环境:values-en-rUS
  3. 中文环境为:values-zh
  4. 大陆地区中文环境: values-zh-cn
    鼠标右键点击res -> New -> Android Resource File
    这里写图片描述
    如果把手机设为英文模式 ,就会自动调用values-en里面的strings.xml
    如果把手机设为中,就会自动调用values里面的里面的strings.xml

横竖屏适配怎么做

res–>layout–>main.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="com.example.a22120.day3_shipei.MainActivity">
<TextView
    android:layout_width="match_parent"
    android:layout_height="@dimen/tv_size"
    android:text="竖屏视频播放区"
    android:textSize="30dp"
    android:gravity="center"
    android:background="#666"
    />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#999"
        >
     <TextView
         android:layout_width="0dp"
         android:layout_height="match_parent"
         android:layout_weight="1"
         android:text="弹幕"
         android:gravity="center"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="登录"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

适配文件的xml文件名字要和布局文件的名字要一样,但是建的文件名要加land
res–>layout-land–>main.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="com.example.a22120.day3_shipei.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="横屏时视频播放区"
        android:textSize="30dp"
        android:gravity="center"
        />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/l_y_j_21/article/details/80600013