android百分比布局失效,Android 百分比布局库【原创】

为了解决android手机适配问题,我们经常想如果可以按照百分比的方式进行界面布局,这样适配各种屏幕就简单多了吧!现在谷歌正式提供百分比布局支持库(android-support-percent-lib)。

这个库提供了:

两种布局供大家使用: PercentRelativeLayout、PercentFrameLayout,通过名字就可以看出,这是继承自FrameLayout和RelativeLayout两个容器类;

支持的属性有:

layout_widthPercent、layout_heightPercent、

layout_marginPercent、layout_marginLeftPercent、

layout_marginTopPercent、layout_marginRightPercent、

layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。

可以看到支持宽高,以及margin。

也就是说,大家只要在开发过程中使用PercentRelativeLayout、PercentFrameLayout替换FrameLayout、RelativeLayout即可。

使用:

使用Android studio在build.gradle添加以下信息就可以获取支持库,当然了,如果你没有下载到该支持库会提示你下载。

5330C3E9-D43A-40E0-A247-83C217BE9A84.bmp

PercentRelativeLayout使用小例子:

布局文件如下:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/row_one_item_one"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_alignParentTop="true"

android:background="#7700ff00"

android:text="w:70%,h:20%"

android:gravity="center"

app:layout_heightPercent="20%"

app:layout_widthPercent="70%"/>

android:id="@+id/row_one_item_two"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_toRightOf="@+id/row_one_item_one"

android:background="#396190"

android:text="w:30%,h:20%"

app:layout_heightPercent="20%"

android:gravity="center"

app:layout_widthPercent="30%"/>

android:id="@+id/row_two_item_one"

android:layout_width="match_parent"

android:layout_height="0dp"

android:scaleType="centerCrop"

android:layout_below="@+id/row_one_item_one"

android:background="#d89695"

app:layout_heightPercent="70%"/>

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_below="@id/row_two_item_one"

android:background="#770000ff"

android:gravity="center"

android:text="width:100%,height:10%"

app:layout_heightPercent="10%"

app:layout_widthPercent="100%"/>

效果图如下

Screenshot_2015-09-16-12-02-58-169x300.jpeg

因为没有LinearLayout我们可以自己来自定义。刚好在网上看到有自定义好的PercentLinearlayout,分享给大家。

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);

}

}

}

使用效果如下:

Screenshot_2015-09-16-12-10-06-169x300.jpeg

参考博客:http://blog.csdn.net/lmj623565791/article/details/46695347

转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/13810.html

75d087ef9a9fb11dc373caaf33adbf7f.png

微信打赏

支付宝打赏

感谢您对作者Lena的打赏,我们会更加努力!    如果您想成为作者,请点我

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值