android UI布局優(yōu)化之merge標(biāo)簽

merge標(biāo)簽可以降低UI層級

1.<merge />只可以作為xml layout的根節(jié)點。
2.當(dāng)需要inflate的xml layout本身是由merge作為根節(jié)點的話,需要將被導(dǎo)入的xml layout置于viewGroup中,同時需要設(shè)置attachToRoot為True。

一個布局可以重復(fù)利用,可以抽成一個view,這個view繼承的viewgroup是其xml的跟布局,這樣xml的根布局就可以用merge替代,

// 繼承RelativeLayout
public class RadioPalyerHistoryView extends RelativeLayout {

    public RadioPalyerHistoryView(@NonNull Context context) {
        this(context, null);
    }

    public RadioPalyerHistoryView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadioPalyerHistoryView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
       // 必須attachToRoot為true
        View view = LayoutInflater.from(context).inflate(R.layout.radio_palyer_history_right_layout, this, true);
// 跟布局是merge標(biāo)簽,在跟布局設(shè)置的padding不管用了,代碼設(shè)置
 setPadding(CommonUtils.dip2px(context,12),CommonUtils.dip2px(context,10),CommonUtils.dip2px(context,12),CommonUtils.dip2px(context,10));
    }

其跟布局先用RelativeLayout,這樣方便寫布局看下子view的布局位置,等寫好布局了,用merge替換

<?xml version="1.0" encoding="utf-8"?>
<!-- 跟布局是RelativeLayout-->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black_27292B"
        android:textSize="15sp"
        tools:text="何以小 第二集"
        android:ellipsize="end"
        android:singleLine="true"/>

    <TextView
        android:id="@+id/tv_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp"
        android:textColor="@color/black_989A9B"
        android:layout_marginTop="5dp"
        tools:text="聽至10%"
        android:layout_below="@id/tv_title"/>

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp"
        android:textColor="@color/black_989A9B"
        android:layout_marginTop="5dp"
        tools:text="4個小時前"
        android:layout_below="@id/tv_title"
        android:layout_toRightOf="@id/tv_progress"
        android:layout_marginLeft="7dp"/>


</merge>

優(yōu)點:減少布局層級;缺點:跟布局merge不能設(shè)置padding(設(shè)置的padding不起作用),可以把設(shè)置在跟布局的padding設(shè)置到代碼;不可以設(shè)置固定值的寬高,例如:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="280dp"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

layout_height="280dp"和:paddingLeft="12dp"這些都不會起作用,需要在代碼中設(shè)置;
原因是:在在LayoutInflater的inflate方法中,

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context) mConstructorArgs[0];
            mConstructorArgs[0] = inflaterContext;
            View result = root;

            try {
            // 省略
                ...
                // merge標(biāo)簽必須有root,attachToRoot為true
                if (TAG_MERGE.equals(name)) {
                    if (root == null || !attachToRoot) {
                        throw new InflateException("<merge /> can be used only with a valid "
                                + "ViewGroup root and attachToRoot=true");
                    }

                    rInflate(parser, root, inflaterContext, attrs, false);
                } else {
                   ... 

            } catch (XmlPullParserException e) {
            ...

            return result;
        }
    }
void rInflate(XmlPullParser parser, View parent, Context context,
            AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {

        final int depth = parser.getDepth();
        int type;
        boolean pendingRequestFocus = false;

        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            final String name = parser.getName();

            if (TAG_REQUEST_FOCUS.equals(name)) {
                pendingRequestFocus = true;
                consumeChildElements(parser);
            } else if (TAG_TAG.equals(name)) {
                parseViewTag(parser, parent, attrs);
            } else if (TAG_INCLUDE.equals(name)) {
                if (parser.getDepth() == 0) {
                    throw new InflateException("<include /> cannot be the root element");
                }
                parseInclude(parser, context, parent, attrs);
            } else if (TAG_MERGE.equals(name)) {
                throw new InflateException("<merge /> must be the root element");
            } else {
                //。 沒有用merge標(biāo)簽的寬高屬性,padding屬性,直接addview到parent中
                final View view = createViewFromTag(parent, name, context, attrs);
                final ViewGroup viewGroup = (ViewGroup) parent;
                final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
                rInflateChildren(parser, view, attrs, true);
                viewGroup.addView(view, params);
            }
        }

        if (pendingRequestFocus) {
            parent.restoreDefaultFocus();
        }

        if (finishInflate) {
            parent.onFinishInflate();
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容