使用Android databinding进行动态布局

来源:互联网 发布:windows to go u盘要求 编辑:程序博客网 时间:2024/05/29 16:47

Google 最近公布了Android mvvm架构,可以使用databinding做些赋值和渲染界面的操作。

了解到Jsp中使用EL表达式,可以做些简单的赋值和表达式运算。

然后我写一个简单的库,可以使用Android的Databinding进行动态布局,布局的大小可以通过表达式来计算。

比如一个view的高度是屏幕宽度的三分之一,然后再减10dp。可以这样使用。

width = "@{dimen.screenWidth/3 - dimen.dp(10)}"

下面是Github连接
AndroidSizeUtil

下面是文档:

AndroidSizeUtil


Desc

AndroidSizeUtil is use android mvvm(databinding) architecture.
It is easy to compose layout, like html. Use percentage or expression in xml file to compose layout.

Usage

#1

Your app project must use android mvvm(databinding) architecture.
Android mvvm doc

#2

Gradle compile

repositories {    maven {        url "https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidSizeUtil/master/repo/"    }}compile 'cn.mycommons:androidsizeutil:1.0.0'

#3

Init AndroidSizeUtil in android Application or Activity.

public class AppContext extends Application {    @Override    public void onCreate() {        super.onCreate();        AndroidSizeUtil.init(this);    }}

#4

Improt AndroidSizeUtil in xml.

<?xml version="1.0" encoding="utf-8"?><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>        <variable            name="dimen"            type="cn.mycommons.androidsizeutil.DimenUtil" />    </data></layout>

#5

Use dimen to compose view or layout like this.

<TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    app:layout_height="@{dimen.csh/3}"    app:layout_marginLeft="@{dimen.csw/3}"    app:layout_marginTop="@{0}"    app:layout_width="@{dimen.csw*2/3}" />

#6

Use databind in activity.
ContentMainBinding is generate java file by android databinding.

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ContentMainBinding.bind(findViewById(R.id.content_main)).setDimen(AndroidSizeUtil.newDimenUtil());    }}

Api

Attributes set width, height, paddding, margin. The unit is px.

app:layout_width // set view widthapp:layout_height // set view heightandroid:paddingLeft // set view paddingLeftandroid:paddingRight // set view paddingRightandroid:paddingTop // set view paddingTopandroid:paddingBottom // set view paddingBottomapp:paddingLeft // set view paddingLeftapp:paddingRight // set view paddingRightapp:paddingTop // set view paddingTopapp:paddingBottom // set view paddingBottomandroid:layout_marginLeft // set view layout_marginLeftandroid:layout_marginRight // set view layout_marginRightandroid:layout_marginTop // set view layout_marginTopandroid:layout_marginBottom // set view layout_marginBottomapp:layout_marginLeft // set view layout_marginLeftapp:layout_marginRight // set view layout_marginRightapp:layout_marginTop // set view layout_marginTopapp:layout_marginBottom // set view layout_marginBottom

Expression to set size.

// 100% screen widthapp:layout_width="@{dimen.sw}"app:layout_width="@{dimen.screenWidth}"// 1/2 screen heightapp:layout_height="@{dimen.sh/2}"app:layout_height="@{dimen.screenHeight/2}"// 100pxapp:layout_width="@{100}"app:layout_width="@{dimen.px(100)}"// 100dpapp:layout_width="@{dimen.dp(100)}"// 100dp - 10pxapp:layout_width="@{dimen.dp(100)-10}"

Set view or layout size.

// 100dp * 100dpapp:size="@{dimen.sizeDp(100,100)}"// 100px * 100pxapp:size="@{dimen.size(100,100)}"// 100dp * 100pxapp:size="@{dimen.size(dimen.dp(100),100)}"// screenWidth * 100pxapp:size="@{dimen.size(dimen.screenWidth,100)}"

Java api.

DimenUtil.java
LayoutUtil.java

Demo

<?xml version="1.0" encoding="utf-8"?><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>        <variable            name="dimen"            type="cn.mycommons.androidsizeutil.DimenUtil" />    </data>    <RelativeLayout        android:id="@+id/content_main"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        app:layout_behavior="@string/appbar_scrolling_view_behavior"        tools:context=".MainActivity"        tools:showIn="@layout/activity_main">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#faa"            android:gravity="center"            android:text="1"            app:size="@{dimen.size(dimen.csw/3,dimen.csh/3)}" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#f00"            android:gravity="center"            android:text="2"            app:layout_height="@{dimen.csh/3}"            app:layout_marginLeft="@{dimen.csw/3}"            app:layout_marginTop="@{0}"            app:layout_width="@{dimen.csw*2/3}" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#f0f"            android:gravity="center"            android:text="3"            app:layout_height="@{dimen.csh*2/3}"            app:layout_marginLeft="@{0}"            app:layout_marginTop="@{dimen.csh/3}"            app:layout_width="@{dimen.csw/3}" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#ff0"            android:gravity="center"            android:text="4"            app:layout_height="@{dimen.csh*2/3}"            app:layout_marginLeft="@{dimen.csw/3}"            app:layout_marginTop="@{dimen.csh/3}"            app:layout_width="@{dimen.csw*2/3}" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="#0f0"            android:gravity="center"            android:text="5"            app:layout_marginLeft="@{dimen.csw/3-dimen.dp(50)}"            app:layout_marginTop="@{dimen.csh/3-dimen.dp(50)}"            app:size="@{dimen.sizeDp(100,100)}" />    </RelativeLayout></layout>

Demo

Gradle

repositories {    maven {        url "https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidSizeUtil/master/repo/"    }}compile 'cn.mycommons:androidsizeutil:1.0.0'
0 0
原创粉丝点击