Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 短信列表界面背景繪製機制原始碼分析

為什麼短信清單有資料時是白底,空清單時卻是另一個背景?答案分散在 Mms 的版面檔與 PhoneWindow 的選單背景機制裡。

一、有清單項目時

入口是 ConversationList.java,版面在 packages/apps/Mms/res/layout/conversation_list_screen.xml:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:background="@android:color/white"
        android:cacheColorHint="@android:color/white"
        android:fadingEdgeLength="16dip" />

    <TextView android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/no_conversations"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

關鍵是 ListView 自己把背景與 cacheColorHint 都設為白色,所以列表滾動時不會露出底層,視覺上是純白。

二、清單為空白時

空清單時改由 DecorView 套用「面板背景」,源頭在 frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindow.java:

public class PhoneWindow extends Window implements MenuBuilder.Callback {
    private void openPanel(PanelFeatureState st, KeyEvent event) {
        int backgroundResId;
        if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
            // 寬度填滿時使用「全寬」背景
            backgroundResId = st.fullBackground;
            width = MATCH_PARENT;
        } else {
            backgroundResId = st.background;
        }
        st.decorView.setWindowBackground(
                getContext().getResources().getDrawable(backgroundResId));
    }

    private final class DecorView extends FrameLayout implements RootViewSurfaceTaker {
        public void setWindowBackground(Drawable drawable) {
            if (getBackground() != drawable) {
                setBackgroundDrawable(drawable);
                if (drawable != null)
                    drawable.getPadding(mBackgroundPadding);
                else
                    mBackgroundPadding.setEmpty();
                drawableChanged();
            }
        }

        @Override
        public void setBackgroundDrawable(Drawable d) {
            super.setBackgroundDrawable(d);
            if (getWindowToken() != null)
                updateWindowResizeState();
        }
    }

    private static final class DrawableFeatureState {
        void setStyle(Context context) {
            TypedArray a = context.obtainStyledAttributes(com.android.internal.R.styleable.Theme);
            background = a.getResourceId(
                    com.android.internal.R.styleable.Theme_panelBackground, 0);
            fullBackground = a.getResourceId(
                    com.android.internal.R.styleable.Theme_panelFullBackground, 0);
        }
    }
}

這兩個屬性在 frameworks/base/core/res/res/values/Themes.xml 中定義:

<item name="panelBackground">@android:drawable/menu_background</item>
<item name="panelFullBackground">@android:drawable/menu_background_fill_parent_width</item>

對應的九宮格圖在 frameworks/base/core/res/res/drawable-hdpi/:

menu_background.9.png
menu_background_fill_parent_width.9.png

結論

因此短信列表有兩套背景來源:有資料時由 ListView 的 android:background 決定(白),空清單時則落到 DecorView 依主題的 panelBackground/panelFullBackground 套用系統九宮格圖。想統一風格,就必須同時改這兩處,只改版面檔會出現「有資料與沒資料長得不一樣」的落差。

Copyright © Linux教程網 All Rights Reserved