Android進階-基于databinding實現(xiàn)更高效的自動布局

至于DataBinding是什么,這里我不再贅述,今天我要做的就是基于它實現(xiàn)自動布局,這么說吧,假設設計圖是按照750x1334設計的,現(xiàn)在的手機分辨率是1500x2668,均是它的兩倍,如此,原來是100x100的圖標,我們應該將它變?yōu)?00x200,而至于原來是100x200這種的,應該變?yōu)?00x400。最后,假設現(xiàn)在的分辨率為2250x2668,即款是以前的3倍,高是以前的兩倍,原來200x200的,在這個設備上,我讓它變?yōu)榱?00x400,不過,這樣做,似乎有問題,難道不應該是400x400或者600x600嗎?至于到底應該是那個,請容以后再議,現(xiàn)在,先來實現(xiàn)600x400的功能吧。

基于Data Binding實現(xiàn)的自動布局

Data Binding里使用Glide加載圖片

具體請看這個問答 如果想要在data binding里使用Glide,你需要在你的model里添加如下代碼:

/**
 * 加載圖片
 */
@BindingAdapter({"imageUrl"})
public static void loadImage(ImageView view, String imageUrl) {
    ImageLoader.load(view, imageUrl);
}

接著在xml里這么寫:

<ImageView
    android:id="@+id/picture"
    app:imageUrl="@{item.url}"
    android:scaleType="fitXY"
    android:src="@mipmap/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

如此,就可以了,至于上面java代碼里的ImageLoader,只是我封裝的圖片加載庫而已!為什么我要提這個呢,因為今天所說的都是基于這個!既然app:imageUrl可以實現(xiàn)圖片的自動加載,那么app:layout_width以及app:layout_height能否實現(xiàn)自動設置寬高呢,答案是肯定的,毫無疑問,這下好了,這等于你的布局文件向你的model開放了一個接口,然后model實現(xiàn)它就可以完成自動布局了。

通過data binding設置控件寬高

java代碼如下:

//  設置寬高
@BindingAdapter("layout_width")
public static void setWidth(View view, int width) {
    if (view == null || width < 0 || !initView()) return;
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (params != null) {
        params.width = (int) (width * scaleWidth);
    }
}

@BindingAdapter("layout_height")
public static void setHeight(View view, int height) {
    if (view == null || height < 0 || !initView()) return;
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (params != null) {
        params.height = (int) (height * scaleHeight);
    }
}

@BindingAdapter({"layout_height", "layout_width"})
public static void setWidthAndHeight(View view, int height, int width) {
    if (view == null || height < 0 || width < 0 || !initView()) return;
    ViewGroup.LayoutParams params = view.getLayoutParams();
    if (params != null) {
        params.height = (int) (height * scaleHeight);
        params.width = (int) (width * scaleWidth);
    }
}

至于xml里,假設你這么定義:android:layout="100px",那么,你接著寫句app:layout_width="@{100}",height類似。然后就可以了。下面是我的xml文件布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto">
    <data class="AutoLayoutView">
        <variable
            name="bean"
            type="com.ijustyce.weekly1601.viewmodel.PersonView" />
    </data>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="716px"
        android:layout_height="716px"
        app:layout_width="@{716}"
        app:layout_height="@{716}"
        android:background="@color/black"
        android:orientation="vertical">

        <Button
            android:id="@+id/first"
            app:layout_width="@{200}"
            app:layout_height="@{200}"
            android:layout_marginTop="258px"
            app:layout_marginTop="@{258}"
            android:textSize="24px"
            android:text="哈哈哈哈"
            app:textSize="@{24}"
            android:layout_width="200px"
            android:layout_height="200px" />

        <Button
            android:layout_toRightOf="@id/first"
            app:layout_width="@{200}"
            app:layout_height="@{200}"
            app:textSize="@{24}"
            android:textSize="24px"
            android:text="哈哈哈哈"
            android:layout_centerVertical="true"
            android:layout_width="200px"
            android:layout_height="200px" />

        <RelativeLayout
            android:paddingLeft="200px"
            app:paddingLeft="@{200}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:textSize="24px"
                app:textSize="@{24}"
                app:layout_width="@{200}"
                app:layout_height="@{200}"
                android:text="哈哈哈哈"
                android:layout_width="200px"
                android:layout_height="200px" />

        </RelativeLayout>

    </RelativeLayout>
</layout>

這部分源碼已經(jīng)上傳至 碼云

待完善

目前,已經(jīng)支持寬高、margin、padding、textsize等屬性,只是支持的并不全面,比如marginStart并不支持,以及本文開始就提到的那個問題,寬是兩倍,高是三倍這樣的機型上該如何?我覺得,可以通過額外的屬性去解決。默認寬按寬計算,高按高計算,只是允許你修改自定義。看了這么多,傻眼了吧,再不學習mvvm你就真的徹底OUT了,毫無疑問!

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

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

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