最近买了几本书在看,其中看到百分比这块,觉得应该记录一下。

我一直在想为什么安卓不能像HTML布局一样通过屏幕宽度的百分比进行布局,这样适配方面会小很多问题,所以在百分比布局刚刚出来的时候我就去研究过,但是谷歌提供的Android系统percent-支持这个库只支持了PercentRelativeLayout,Percent Layout两种布局,没有支持LinearLayout,在书上看到Google为了支持LinearLayout然后在android-percent-support这个库原来基础上进行了拓展,看到这个消息感觉很嗨,感觉终于可以全面的使用在项目中了。

废话不多,我们来看看这几个的使用吧!

一、PercentRelativeLayout 继承RelativeLayout,支持属性有:设置view的宽高、边距、位置、宽高比具体方法如下:

 

  • <! - 相对于父控件的宽度的百分比 - >
  • 应用:layout_widthPercent
  • <! - 相对于父控件的高度的百分比 - >
  • 应用:layout_heightPercent
  • <! - 根据比例设置该控件相对于父控件的边距 - >
  • 应用:layout_marginLeftPercent
  • 应用:layout_marginTopPercent
  • 应用:layout_marginRightPercent
  • 应用:layout_marginBottomPercent
  • <! - 距离开始和结束的位置 - >
  • 应用:layout_marginStartPercent
  • 应用:layout_marginEndPercent
  • <! - 根据宽或者高中的一个来计算另一个的值 - >
  • 机器人:layout_width
  • 应用:layout_aspectRatio
<?  version=\"1.0\" encoding=\"utf-8\"?>
<android.support.percent.PercentRelativeLayout  ns:android=\"http://schemas.android.com/apk/res/android\"
     ns:tools=\"http://schemas.android.com/tools\"
     ns:app=\"http://schemas.android.com/apk/res-auto\"
    android:id=\"@+id/activity_layout_test\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\"
    tools:context=\"com.example.ws.scrollviewdemo.LayoutTestActivity\">


    <TextView
        android:layout_alignParentTop=\"true\"
        android:background=\"#7700ff\"
        android:text=\"蓝色\"
        android:gravity=\"center\"
        app:layout_marginEndPercent=\"30%\"
        app:layout_widthPercent=\"70%\"
        app:layout_heightPercent=\"20%\"
        />
    <TextView
        android:layout_alignParentTop=\"true\"
        android:layout_alignParentRight=\"true\"
        android:background=\"#feee33\"
        android:text=\"黄色\"
        android:gravity=\"center\"
        app:layout_marginTopPercent=\"30%\"
        app:layout_marginEndPercent=\"10%\"
        app:layout_widthPercent=\"30%\"
        app:layout_heightPercent=\"20%\"
        />
    <TextView
        android:layout_height=\"300dp\"
        android:background=\"#7700ff00\"
        android:text=\"绿色\"
        android:gravity=\"center\"
        app:layout_aspectRatio = \"60%\"
        android:layout_alignParentBottom=\"true\"/>

    <TextView
        app:layout_widthPercent=\"10%\"
        app:layout_heightPercent=\"10%\"
        android:layout_alignParentBottom=\"true\"
        android:layout_alignParentRight=\"true\"
        android:background=\"#ff6b00\"
        android:text=\"大小不合适怎么办不知道呢哈哈哈\"/>

    <TextView
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\"
        app:layout_widthPercent=\"10%\"
        app:layout_heightPercent=\"10%\"
        android:layout_alignParentBottom=\"true\"
        android:layout_alignParentRight=\"true\"
        android:background=\"#ff6eee\"
        android:text=\"大小不合适怎么办不知道呢哈哈哈\"
        app:layout_marginBottomPercent=\"15%\"/>

</android.support.percent.PercentRelativeLayout>

 

\"\"

 

二,Percent Layout集成 Layou 支持属性有:设置视图的宽高,边距,位置,宽高比具体方法如下

 

  • <! - 相对于父控件的宽度的百分比 - >
  • 应用:layout_widthPercent
  • <! - 相对于父控件的高度的百分比 - >
  • 应用:layout_heightPercent
  • <! - 根据比例设置该控件相对于父控件的边距 - >
  • 应用:layout_marginLeftPercent
  • 应用:layout_marginTopPercent
  • 应用:layout_marginRightPercent
  • 应用:layout_marginBottomPercent
  • <! - 距离开始和结束的位置 - >
  • 应用:layout_marginStartPercent
  • 应用:layout_marginEndPercent
  • <! - 根据宽或者高中的一个来计算另一个的值 - >
  • 机器人:layout_width
  • 应用:layout_aspectRatio
<?  version=\"1.0\" encoding=\"utf-8\"?>
<android.support.percent.Percent Layout  ns:android=\"http://schemas.android.com/apk/res/android\"
     ns:app=\"http://schemas.android.com/apk/res-auto\"
    android:layout_width=\"match_parent\"
    android:layout_height=\"match_parent\">

    <Button
        android:background=\"@android:color/holo_green_light\"
        app:layout_heightPercent=\"45%\"
        app:layout_widthPercent=\"100%\" />

    <Button
        android:layout_gravity=\"center\"
        android:background=\"@android:color/darker_gray\"
        app:layout_heightPercent=\"10%\"
        app:layout_widthPercent=\"100%\" />

    <Button
        android:layout_gravity=\"bottom|left\"
        android:background=\"@android:color/holo_blue_dark\"
        app:layout_heightPercent=\"45%\"
        app:layout_widthPercent=\"50%\" />

    <Button
        android:layout_gravity=\"bottom|right\"
        android:background=\"@android:color/holo_blue_bright\"
        app:layout_heightPercent=\"45%\"
        app:layout_marginLeftPercent=\"30%\"
        app:layout_widthPercent=\"50%\" />

</android.support.percent.Percent Layout>

\"\"

 

三,Percent LinearLayout  继承LinearLayout支持属性 

PercentRelativeLayout与Percent Layout出来后,就有Android大神根据google的提供出现了Percent LinearLayout,使用方法与PercentRelativeLayout相似,具体可以看鸿阳的博客。

 

 

 

 

收藏 打印