android support Percent支持库开发

Android的布局支持百分比的设置进行开发,来学习如何去实现它,不过看起来会像网页的设置,比如宽度的设置属性是`layout_widthPercent`。在此之前,我们一般都会设置Linearlayout的weight权重来实现布局间的比例大小。

Percent support Library提供了两个新的类:

1.PercentRelativeLayout

2.PercentFrameLayout

# 创建新项目 


创建一个新的项目来测试,修改`build.gradle`,需要引入以下库 

    applyplugin:'com.android.application'

    android {

    compileSdkVersion23

    buildToolsVersion"23.0.0"

    defaultConfig {

    applicationId"com.android.chaowen.percentdemo1"

minSdkVersion7

targetSdkVersion22

versionCode1

versionName"1.0"

}

buildTypes {

release {

minifyEnabledfalse

proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'

}

}

}

dependencies {

compile fileTree(dir:'libs',include: ['*.jar'])

compile'com.android.support:support-annotations:23.0.0'

compile'com.android.support:appcompat-v7:23.0.0'

compile'com.android.support:design:23.0.0'

compile'com.android.support:percent:23.0.0'

}


# 创建布局

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"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/first"

android:background="@color/sa_green_dark"

app:layout_heightPercent="50%"

app:layout_marginLeftPercent="25%"

app:layout_marginTopPercent="25%"

app:layout_widthPercent="50%"/>

android:layout_width="0dp"

android:layout_height="32dp"

android:layout_alignLeft="@id/first"

android:layout_alignStart="@id/first"

android:layout_alignRight="@id/first"

android:layout_alignEnd="@id/first"

android:layout_below="@id/first"

android:layout_marginTop="8dp"

android:background="@color/light_grey"/>



比例大小是通过`heightPercent`和`widthPercent`属性来设置百分比大小值。完全属于`RelativeLayout`的扩展类,值得一提的是,

不再需要设置`layout_width`和`layout_height`,注意了,这是新的库使用方法,因为这两个属性会被自动加入了。

百度比也可以用于设置边距。唯一区别的只是采取了百分比值。

最后一点需要注意,在第二个view,并没有直接设置比例的大小,但是它的位置是相对于第一个view.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,896评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,789评论 2 45
  • 带着对风凰美景的期待,踏上了去往风凰的路程。初入眼帘的是好似古装拍摄现场那城门般的收费站。灰色的墙壁金色的牌子。那...
    喜欢羊的牛阅读 4,068评论 0 3
  • 安凯客车。汉庭。7天。维也纳。 解放。东风。rav4, 南海狮山。大运。天龙。小松,卡车需求。 广州钢铁交易中心。...
    梦见你了阅读 1,562评论 0 0
  • 村上的小说挪威的森林里头,有个叫永泽的男生。他愤慨世人不努力还满腹牢骚的时候,大概说了这样一句:世上的人都在辛辛苦...
    ac25ad582d12阅读 3,863评论 0 0

友情链接更多精彩内容