DatabindPercent
github: https://github.com/lvzhihao100/DataBindPercent
簡書:http://www.reibang.com/p/ccbfab7e9557
DatabindPercent 使用DataBinding 實(shí)現(xiàn)android 百分比布局,使用簡單方便
百分比庫使用的依賴
dependencies {
compile 'com.github.lvzhihao100:DataBindPercent:1.01'
}
- 首先在Application中初始化
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
WindowUtil.init(this);
}
}
- 在項(xiàng)目的module build.gradle中開啟databinding
android {
......
dataBinding {
enabled = true
}
}
- 更改布局文件格式為databinding格式(根布局layout)驼鞭,例如activity_main
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class=".MainActivityCustom">
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<Button
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_height="@{200}"
app:layout_width="@{320}"
android:textSize="@{30}"
android:text="DatabindPercent"
android:textAllCaps="false"
android:layout_margin="@{50}"
android:background="#ff0000"
/>
</LinearLayout>
</layout>
- 在MainActivity中使用databinding方式
// setContentView(R.layout.activity_main); 以前的方式
DataBindingUtil.setContentView(this,R.layout.activity_main);//databinding的方式
- 根據(jù)上邊的設(shè)置,你會(huì)發(fā)現(xiàn)button的寬度占據(jù)了屏幕的一半,
這是因?yàn)槲以O(shè)置的默認(rèn)滿百分比是640,屏幕的寬度
button的寬度是app:layout_width="@{320}"下愈,占屏幕寬度的320/640,就是1/2,一半兵多。
當(dāng)然這個(gè)屏幕寬度百分之百尺寸,你可以自己設(shè)置的
在初始化的時(shí)候
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
WindowUtil.init(this,屏幕寬,屏幕高);
}
}
百分比的實(shí)現(xiàn)是以屏幕寬為基準(zhǔn)的鸠窗,屏幕高隨意
-
可以使用百分比的有
"android:layout_margin" "android:padding" "android:textSize" "app:layout_width" "app:layout_height" "android:paddingLeft" "android:paddingRight" "android:paddingTop" "android:paddingBottom" "android:layout_marginLeft" "android:layout_marginRight" "android:layout_marginTop" "android:layout_marginBottom" "android:maxHeight" "android:maxWidth"
注意
"app:layout_width" "app:layout_height"
代表控件的寬高,與
"android:layout_width" "android:layout_height"
效果一樣胯究,但"android:layout_width" "android:layout_height"仍需設(shè)置