百分比布局

在学习“第二行代码”3.3.4百分比布局时,我照着文章所述,将代码都写了一遍,发现居然引用“compile ‘com.android.support:percent:24.2.1’”,我就奇怪了!!


真是出师不利啊!!
后来所幸看了源码后,才知道这个是因为我自己的原因,后来改过后,才能顺利进入下一步!

修改后,如图:

或者是这样:

然后看布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_percent_layout" android:layout_width="match_parent" android:layout_height="match_parent" >

    <TextView  android:id="@+id/tv_test1" android:text="认" android:background="@android:color/holo_blue_light" android:layout_gravity="top|left" android:gravity="center" app:layout_widthPercent="30%" app:layout_heightPercent="30%"/>
    <TextView  android:id="@+id/tv_test2" android:text="努" android:background="@android:color/holo_orange_light" android:layout_gravity="top|right" android:gravity="center" app:layout_widthPercent="70%" app:layout_heightPercent="70%"/>

    <TextView  android:id="@+id/tv_test3" android:text="真" android:background="@android:color/holo_green_light" android:layout_gravity="bottom|left" android:gravity="center" app:layout_widthPercent="50%" app:layout_heightPercent="70%"/>
    <TextView  android:id="@+id/tv_test4" android:text="力" android:background="@android:color/holo_red_light" android:layout_gravity="bottom|right" android:gravity="center" app:layout_widthPercent="70%" app:layout_heightPercent="50%"/>

</android.support.percent.PercentFrameLayout>

百分比布局支持PerecentRelativeLayout和PercentFrameLayout,可以理解为只是在宽高上面使用百分比来代替了具体的值和“wrap_content”,然后和RelativeLayout(FrameLayout)属性完全相同。但是百分比是需要自定义命名空间,如代码部分。

预览效果

最后的效果图:

就这么简单! 郭神说的是,该布局兼容Android所有系统版本!!!赶紧试一试,看看还没有坑?

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...