Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 多機型屏幕適配完整解決方案

編輯:關於 Android 程式設計

目前市場上 Android 機型繁多,如何讓自己的應用能夠更多地適配不同機型,是一個非常現實且必須處理的問題。Android 官方提供的解決文檔是從三個方向來分析處理的。

(一)支援不同屏幕大小的設備

1.1 合理使用 wrap_content 與 match_parent;
1.2 盡可能使用 RelativeLayout;
1.3 針對不同機型使用不同的布局檔案(Use Size Qualifiers):

res/layout/main.xml
res/layout-large/main.xml

1.4 盡量使用點 9 圖片(Nine-patch Bitmaps)。

(二)支援不同屏幕密度的設備

2.1 使用與密度無關的像素單位(Use Density-independent Pixels)——即 dp、sp:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/clickme"
    android:layout_marginTop="20dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp" />

2.2 為不同設備提供可選擇的圖片(Provide Alternative Bitmaps)。不同解析度之間的比例關係為:

xhdpi: 2.0
hdpi: 1.5
mdpi: 1.0(baseline)
ldpi: 0.75

以 awesomeimage.png 為例,圖片的擺放位置如下:

MyProject/
res/
  drawable-xhdpi/
    awesomeimage.png
  drawable-hdpi/
    awesomeimage.png
  drawable-mdpi/
    awesomeimage.png
  drawable-ldpi/
    awesomeimage.png

(三)實作自適應用戶介面流(Adaptative UI Flows)

3.1 識別目前的布局檔案是哪一個(Determine the Current Layout):主要採用標誌位的方式來取得目前使用的布局。

public class NewsReaderActivity extends FragmentActivity {
    boolean mIsDualPane;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        View articleView = findViewById(R.id.article);
        mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;
    }
}

針對目前布局中沒有的控件(例如 button),要判斷其是否為 null:

Button catButton = (Button) findViewById(R.id.categorybutton);
OnClickListener listener = /* create your listener here */;
if (catButton != null) {
    catButton.setOnClickListener(listener);
}

3.2 根據目前布局,使用不同的響應操作(React According to Current Layout):

public void onHeadlineSelected(int index) {
    mArtIndex = index;
    if (mIsDualPane) {
        /* display article on the right pane */
        mArticleFragment.displayArticle(mCurrentCat.getArticle(index));
    } else {
        /* start a separate activity */
        Intent intent = new Intent(this, ArticleActivity.class);
        intent.putExtra("catIndex", mCatIndex);
        intent.putExtra("artIndex", index);
        startActivity(intent);
    }
}
final String CATEGORIES[] = { "Top Stories", "Politics", "Economy", "Technology" };

public void onCreate(Bundle savedInstanceState) {
    ....
    if (mIsDualPane) {
        /* use tabs for navigation */
        actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS);
        int i;
        for (i = 0; i < CATEGORIES.length; i++) {
            actionBar.addTab(actionBar.newTab().setText(CATEGORIES[i]).setTabListener(handler));
        }
        actionBar.setSelectedNavigationItem(selTab);
    } else {
        /* use list navigation (spinner) */
        actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_LIST);
        SpinnerAdapter adap = new ArrayAdapter(this, R.layout.headline_item, CATEGORIES);
        actionBar.setListNavigationCallbacks(adap, handler);
    }
}

3.3 在其他 Activity 中復用 Fragments(Reuse Fragments in Other Activities):Android 提供了一個解決「同一操作、不同響應」的方案,就是在父類中定義一個 interface,並在子類中實作,以實作不同操作:

public class HeadlinesFragment extends ListFragment {
    ...
    OnHeadlineSelectedListener mHeadlineSelectedListener = null;

    /* Must be implemented by host activity */
    public interface OnHeadlineSelectedListener {
        public void onHeadlineSelected(int index);
    }
    ...
    public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) {
        mHeadlineSelectedListener = listener;
    }
}
public class HeadlinesFragment extends ListFragment {
    ...
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mHeadlineSelectedListener) {
            mHeadlineSelectedListener.onHeadlineSelected(position);
        }
    }
    ...
}

原始資料

1. Designing for Multiple Screens:
https://developer.android.com/training/multiscreen/index.html

2. Supporting Different Screen Sizes:
https://developer.android.com/training/multiscreen/screensizes.html

3. Supporting Different Screen Densities:
https://developer.android.com/training/multiscreen/screendensities.html

4. Implementing Adaptative UI Flows:
https://developer.android.com/training/multiscreen/adaptui.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved