天天看點

螢幕适配(尺寸機關、圖檔、文字、布局)

1.如何使用尺寸機關适配

  • 在res下建立valus-960x540、values->1184x720檔案夾,分别建立名為dimens.xml的檔案。
  • 為每個dimens.xml建立一個标簽,name為app_width,值分别為100dp和800dp。
  • 在activity_main.xml中建立一個Button,設定其寬度時,使用android:layout_width=”@dimen/app_width”.
  • 分别在正常分辨率、960*540分辨率、1184*720分辨率下運作,觀察Button的寬帶變化。
    螢幕适配(尺寸機關、圖檔、文字、布局)
    螢幕适配(尺寸機關、圖檔、文字、布局)
    分别在裡面建立一個标簽,name為app_width,值為100dp和800dp
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="app_width">500dp</dimen>
</resources>
           
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="app_width">300dp</dimen>
</resources>
           
螢幕适配(尺寸機關、圖檔、文字、布局)
螢幕适配(尺寸機關、圖檔、文字、布局)

第3步注意的是寬度時@dimen/app_width

螢幕适配(尺寸機關、圖檔、文字、布局)

這裡我通過兩個模拟器 分辨率分别是2016x1080和1920x1080

效果圖對比

螢幕适配(尺寸機關、圖檔、文字、布局)
螢幕适配(尺寸機關、圖檔、文字、布局)

明顯上一個按鈕更長

2.什麼是圖檔适配

  • drawable-hapi:運作在密度為hdpi的裝置時,加載此檔案夾。
  • drawable-hapi:運作在密度為ldpi的裝置時,加載此檔案夾
  • drawable-hapi:運作在密度為mdpi的裝置時,加載此檔案夾。
  • drawable-hapi:運作在密度為xdpi的裝置時,加載此檔案夾。
  • drawable-hapi:運作在密度為xxdpi的裝置時,加載此檔案夾。

    -

    這裡我就用fruit 代替所有圖檔 把圖檔分别放在hdpi、ldpi、mdpi中,

    螢幕适配(尺寸機關、圖檔、文字、布局)

而實際 我用兩個模拟器是

螢幕适配(尺寸機關、圖檔、文字、布局)

他們運作後的結果 不同 效果為:

螢幕适配(尺寸機關、圖檔、文字、布局)
螢幕适配(尺寸機關、圖檔、文字、布局)

3.什麼是文字适配?

當手機語言為英文時,APP内的字元串顯示英文

當手機語言為中文時,APP内的字元顯示中文

第一步 ,在res裡 建立一個 values-en 包;

第二步,在res裡本身的String.xml 複制到建立的包内

第三步,修改裡面的内容

螢幕适配(尺寸機關、圖檔、文字、布局)
螢幕适配(尺寸機關、圖檔、文字、布局)

在activity_main.xml裡

代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.xdw.myapplication.MainActivity">

   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="@string/app_title"
       android:textSize="45dp"
       android:gravity="center"
       />
</android.support.constraint.ConstraintLayout>
           

效果如圖:為系統為中文時:

螢幕适配(尺寸機關、圖檔、文字、布局)
螢幕适配(尺寸機關、圖檔、文字、布局)

系統為英文時:

螢幕适配(尺寸機關、圖檔、文字、布局)
螢幕适配(尺寸機關、圖檔、文字、布局)

4.什麼是布局适配?

  • 在res下新增layout-land和layout-port檔案夾,分别建立名為activity_main.xml的布局檔案
  • 為每個activity_main.xml編寫不同内容,能夠區分即可。
  • 在Activity中setContentView(R.layout.activity_main.xml)。
  • 在豎屏時,觀察加載了哪個布局。
  • 在橫屏是,觀察加載了哪個布局。
    螢幕适配(尺寸機關、圖檔、文字、布局)
    螢幕适配(尺寸機關、圖檔、文字、布局)
    下面是運作後的橫豎屏效果:
    螢幕适配(尺寸機關、圖檔、文字、布局)
    螢幕适配(尺寸機關、圖檔、文字、布局)