至于DataBinding是什么,這里我不再贅述丁屎,今天我要做的就是基于它實(shí)現(xiàn)自動(dòng)布局鲁沥,這么說(shuō)吧呼股,假設(shè)設(shè)計(jì)圖是按照750x1334設(shè)計(jì)的,現(xiàn)在的手機(jī)分辨率是1500x2668画恰,均是它的兩倍彭谁,如此,原來(lái)是100x100的圖標(biāo)允扇,我們應(yīng)該將它變?yōu)?00x200缠局,而至于原來(lái)是100x200這種的,應(yīng)該變?yōu)?00x400蔼两。最后甩鳄,假設(shè)現(xiàn)在的分辨率為2250x2668逞度,即款是以前的3倍额划,高是以前的兩倍,原來(lái)200x200的档泽,在這個(gè)設(shè)備上俊戳,我讓它變?yōu)榱?00x400,不過(guò)馆匿,這樣做抑胎,似乎有問(wèn)題,難道不應(yīng)該是400x400或者600x600嗎渐北?至于到底應(yīng)該是那個(gè)阿逃,請(qǐng)容以后再議,現(xiàn)在赃蛛,先來(lái)實(shí)現(xiàn)600x400的功能吧恃锉。
基于Data Binding實(shí)現(xiàn)的自動(dòng)布局
Data Binding里使用Glide加載圖片
具體請(qǐng)看這個(gè)問(wèn)答 如果想要在data binding里使用Glide,你需要在你的model里添加如下代碼:
/**
* 加載圖片
*/
@BindingAdapter({"imageUrl"})
public static void loadImage(ImageView view, String imageUrl) {
ImageLoader.load(view, imageUrl);
}
接著在xml里這么寫(xiě):
<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,只是我封裝的圖片加載庫(kù)而已歧蒋!為什么我要提這個(gè)呢土砂,因?yàn)榻裉焖f(shuō)的都是基于這個(gè)!既然app:imageUrl可以實(shí)現(xiàn)圖片的自動(dòng)加載谜洽,那么app:layout_width以及app:layout_height能否實(shí)現(xiàn)自動(dòng)設(shè)置寬高呢萝映,答案是肯定的,毫無(wú)疑問(wèn)阐虚,這下好了序臂,這等于你的布局文件向你的model開(kāi)放了一個(gè)接口,然后model實(shí)現(xiàn)它就可以完成自動(dòng)布局了敌呈。
通過(guò)data binding設(shè)置控件寬高
java代碼如下:
// 設(shè)置寬高
@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里贸宏,假設(shè)你這么定義:android:layout="100px"造寝,那么,你接著寫(xiě)句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并不支持箩绍,以及本文開(kāi)始就提到的那個(gè)問(wèn)題孔庭,寬是兩倍,高是三倍這樣的機(jī)型上該如何材蛛?我覺(jué)得圆到,可以通過(guò)額外的屬性去解決。默認(rèn)寬按寬計(jì)算卑吭,高按高計(jì)算芽淡,只是允許你修改自定義《股停看了這么多挣菲,傻眼了吧,再不學(xué)習(xí)mvvm你就真的徹底OUT了掷邦,毫無(wú)疑問(wèn)白胀!