Android 百分比布局

Android 百分比布局

开发工具 Android Studio 3.4.1

Android SDK 版本 29.0.1

1.第一步

打开app/build.gradle文件,在dependencies闭包中添加如下内容
Android 百分比布局_第1张图片
与《Android 第一行代码》不同的是,书上添加的是:
compile 'com.android.support:percent:24.2.1'

而系统会提醒你将’compile’替换为’implementation’或者’api’,最后的版本号是你的SDK版本
添加完之后会出现下图所示,点击Sync Now同步即可
在这里插入图片描述

2.第二步

修改xml文件中的代码,如下


<androidx.percentlayout.widget.PercentFrameLayout
    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">
    <Button
        android:id="@+id/button_1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Buton1"
        android:textIsSelectable="true"/>
    <Button
        android:id="@+id/button_2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button2"
        android:textIsSelectable="true"/>
    <Button
        android:id="@+id/button_3"
        android:layout_gravity="left|bottom"
        android:text="Button3"
        android:textIsSelectable="true"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button_4"
        android:layout_gravity="right|bottom"
        android:text="Button4"
        android:textIsSelectable="true"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%" />
androidx.percentlayout.widget.PercentFrameLayout>

《Android 第一行代码》中的修改如下,注意与上方代码块不同之处是头和尾


<android.support.percent.PercentFrameLayout
    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">
    <Button
        android:id="@+id/button_1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Buton1"
        android:textIsSelectable="true"/>
    <Button
        android:id="@+id/button_2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        android:text="Button2"
        android:textIsSelectable="true"/>
    <Button
        android:id="@+id/button_3"
        android:layout_gravity="left|bottom"
        android:text="Button3"
        android:textIsSelectable="true"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>
    <Button
        android:id="@+id/button_4"
        android:layout_gravity="right|bottom"
        android:text="Button4"
        android:textIsSelectable="true"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%" />
android.support.percent.PercentFrameLayout>

当完成第一步,再按照书上所讲修改xml中的代码时,会出现如下图所示问题
Android 百分比布局_第2张图片
按照第二步第一块代码运行如下
Android 百分比布局_第3张图片

你可能感兴趣的:(Android)