[置顶] 使用Android databinding进行动态布局

Kay16W 9年前

来自: http://blog.csdn.net//guijiaoba/article/details/49182577


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 width app:layout_height // set view height android:paddingLeft // set view paddingLeft android:paddingRight // set view paddingRight android:paddingTop // set view paddingTop android:paddingBottom // set view paddingBottom app:paddingLeft // set view paddingLeft app:paddingRight // set view paddingRight app:paddingTop // set view paddingTop app:paddingBottom // set view paddingBottom android:layout_marginLeft // set view layout_marginLeft android:layout_marginRight // set view layout_marginRight android:layout_marginTop // set view layout_marginTop android:layout_marginBottom // set view layout_marginBottom app:layout_marginLeft // set view layout_marginLeft app:layout_marginRight // set view layout_marginRight app:layout_marginTop // set view layout_marginTop app:layout_marginBottom // set view layout_marginBottom

Expression to set size.

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

Set view or layout size.

// 100dp * 100dp  app:size="@{dimen.sizeDp(100,100)}"    // 100px * 100px  app:size="@{dimen.size(100,100)}"      // 100dp * 100px  app:size="@{dimen.size(dimen.dp(100),100)}"      // screenWidth * 100px  app: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'