使用databinding,实现简单全面的百分比布局(屏幕适配的大招)

DatabindPercent

github: https://github.com/lvzhihao100/DataBindPercent
简书:http://www.jianshu.com/p/ccbfab7e9557
DatabindPercent 使用DataBinding 实现android 百分比布局,使用简单方便

!

百分比库使用的依赖

dependencies {
                compile 'com.github.lvzhihao100:DataBindPercent:1.01'
    }
  1. 首先在Application中初始化
public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        WindowUtil.init(this);
    }
}
  1. 在项目的module build.gradle中开启databinding
android {
    ......
    dataBinding {
        enabled = true
    }
}
  1. 更改布局文件格式为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>
  1. 在MainActivity中使用databinding方式
//        setContentView(R.layout.activity_main); 以前的方式

DataBindingUtil.setContentView(this,R.layout.activity_main);//databinding的方式
  1. 根据上边的设置,你会发现button的宽度占据了屏幕的一半,
    这是因为我设置的默认满百分比是640,屏幕的宽度
    button的宽度是app:layout_width=”@{320}”,占屏幕宽度的320/640,就是1/2,一半。
    当然这个屏幕宽度百分之百尺寸,你可以自己设置的
    在初始化的时候
public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        WindowUtil.init(this,屏幕宽,屏幕高);
    }
}

百分比的实现是以屏幕宽为基准的,屏幕高随意

  1. 可以使用百分比的有

    "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”仍需设置

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值