Android 百分比布局讲解
来源:互联网 发布:安卓6.0可用java模拟器 编辑:程序博客网 时间:2024/05/22 11:49
1 前沿
android 推出了百分比布局,有了这个布局我们按照百分比来分配屏幕。大家是不是听了很激动?下面我们来慢慢讲解。
2 控件介绍
2.1 控件介绍
我们来看一下这几个百分比控件:
1. PercentRelativeLayout2. PercentFrameLayout.
它们分别继承了FrameLayout和RelativeLayout两个容器类。大家可能奇怪为什么没有PercentLinearLayout控件呢?这个android确实没给出,但是我们可以自己做一个嘛!
2.2 支持的属性
layout_widthPercent、layout_heightPercent、 layout_marginPercent、layout_marginLeftPercent、 layout_marginTopPercent、layout_marginRightPercent、 layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent
上面这些是百分比宽度和margin属性,具体使用请继续看。
3 使用步骤
1. 在build.gradle添加:
compile 'com.android.support:percent:22.2.0'
2. 使用控件
<?xml version="1.0" encoding="utf-8"?><android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <Button android:id="@+id/button1" android:text="左按钮" android:background="#FBAFB0" android:layout_alignParentLeft="true" android:layout_width="0dp" android:layout_height="0dp" app:layout_heightPercent="100%" app:layout_widthPercent="70%"/> /> <Button android:id="@+id/button2" android:text="右按钮" android:background="#A5F4AF" android:layout_alignParentRight="true" android:layout_width="0dp" android:layout_height="0dp" app:layout_heightPercent="100%" app:layout_widthPercent="30%" /></android.support.percent.PercentRelativeLayout>
3. 效果图
是不是很简单啊。下面我们再自己做一个PercentLinearLayout.
4 PercentLinearLayout代码
import android.content.Context;import android.content.res.TypedArray;import android.support.percent.PercentLayoutHelper;import android.util.AttributeSet;import android.view.ViewGroup;import android.widget.LinearLayout;/** * Created by Yyd on 2017-01-09. */public class PercentLinearLayout extends LinearLayout { private PercentLayoutHelper mPercentLayoutHelper; public PercentLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); mPercentLayoutHelper = new PercentLayoutHelper(this); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (mPercentLayoutHelper.handleMeasuredStateTooSmall()) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); mPercentLayoutHelper.restoreOriginalParams(); } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new LayoutParams(getContext(), attrs); } public static class LayoutParams extends LinearLayout.LayoutParams implements PercentLayoutHelper.PercentLayoutParams { private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo; public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs); } @Override public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo() { return mPercentLayoutInfo; } @Override protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr); } public LayoutParams(int width, int height) { super(width, height); } public LayoutParams(ViewGroup.LayoutParams source) { super(source); } public LayoutParams(MarginLayoutParams source) { super(source); } }}
5 结尾
注:本文参照鸿洋大神写的文章,因源码我还没有研究明白,所以只记录如何使用。等我研究明白再补充。
6 参考文档
1.Android 百分比布局库(percent-support-lib) 解析与扩展
2.PercentRelativeLayout 源码解析
0 0
- Android 百分比布局讲解
- Android 支持百分比布局
- Android支持百分比布局
- Android百分比布局初探
- Android百分比布局:PercentRelativeLayout
- Android百分比布局:PercentFrameLayout
- Android 百分比布局
- android 百分比layout布局
- Android百分比布局
- android 百分比布局
- Android 百分比布局库
- Android 百分比布局揭秘
- Android 自定义百分比布局
- Android百分比布局
- android中的百分比布局
- Android百分比布局:PercentFrameLayout
- Android百分比布局:PercentRelativeLayout
- android百分比布局
- Hibernate----环境搭建
- LeetCode 8 String to Integer (atoi)
- Xamarin开发教程如何使用Xamarin开发Android应用
- Thread.currentThread().getName() 和 this.getName()区别详解
- java 监听器和监听器模式
- Android 百分比布局讲解
- OpenGL ES 2.0 - 绘制方式
- Android开发之RecyclerView优秀文集,从入门到精通
- 浅谈 SQL 中的锁(一)余额问题
- R语言学习六
- 关于整合springmvc的时候出现的两个磨人的问题
- 数值分析 第三章 线性方程组的迭代法
- Mutable Access of To-Many Relationships
- eclipse和as 工具下载