Android Studio 3.5.1 在项目的build.gradle中添加百分比布局库

本文介绍如何在Android Studio 3.5.1中使用百分比布局库,通过在项目的build.gradle添加依赖,实现灵活的响应式布局设计。利用Guideline控件设定水平和垂直方向的百分比指导线,配合ConstraintLayout约束布局,实现按钮等控件的位置精确控制。

参考文章链接:

Android Studio 3.5.1 在项目的build.gradle中添加百分比布局库

总结:

直接在xml文件中如下使用百分比布局即可

具体布局根据自己需求再去更改

<androidx.constraintlayout.widget.ConstraintLayout
         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">

     <androidx.constraintlayout.widget.Guideline
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/left_guideline"
         app:layout_constraintGuide_percent=".15"
         android:orientation="vertical"/>

     <androidx.constraintlayout.widget.Guideline
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/right_guideline"
         app:layout_constraintGuide_percent=".85"
         android:orientation="vertical"/>

     <androidx.constraintlayout.widget.Guideline
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/top_guideline"
         app:layout_constraintGuide_percent=".15"
         android:orientation="horizontal"/>

     <androidx.constraintlayout.widget.Guideline
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/bottom_guideline"
         app:layout_constraintGuide_percent=".85"
         android:orientation="horizontal"/>

     <Button
         android:text="Button"
         android:layout_width="0dp"
         android:layout_height="0dp"
         android:id="@+id/button"
         app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
         app:layout_constraintRight_toRightOf="@+id/right_guideline"
         app:layout_constraintTop_toTopOf="@+id/top_guideline"
         app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" />

 </androidx.constraintlayout.widget.ConstraintLayout>

以上代码对应的结果如图:

在这里插入图片描述

Android Studio 项目中,`build.gradle` 文件用于管理项目的构建配置,其中 `dependencies` 块用于声明模块所需的依赖项。根据作用域和位置,`build.gradle` 文件分为两个级别:项目级(Project-level)和模块级(Module-level)。两者都可以包含 `dependencies` 配置,但用途不同。 ### 一、项目build.gradle 的 dependencies 项目级的 `build.gradle` 主要用于声明整个项目范围内使用的 Gradle 插件版本,包括 Android Gradle 插件等。这些依赖通常以 `classpath` 的形式出现[^3]。 例如: ```groovy buildscript { repositories { google() mavenCentral() } dependencies { classpath &#39;com.android.tools.build:gradle:7.2.1&#39; // 或使用最新版本 } } ``` 该配置决定了 Android 构建工具的行为,并不会影响应用运行时的依赖。 ### 二、模块级 build.gradle 的 dependencies 模块级的 `build.gradle` 定义了该模块所依赖的库文件,这些依赖最终会被打包进 APK 中或作为编译时辅助工具使用。常见的依赖配置有: - **implementation**:编译并打包到 APK 中。 - **api**(旧版):与 `implementation` 类似,但在多模块项目中会暴露给其他模块。 - **compileOnly**:仅在编译时使用,不打包进 APK。 - **runtimeOnly**:仅在运行时使用,不参与编译。 - **annotationProcessor**:用于处理注解处理器。 - **androidTestImplementation** 和 **testImplementation**:分别用于 Android 测试和本地单元测试。 示例配置如下: ```groovy dependencies { implementation &#39;androidx.core:core-ktx:1.9.0&#39; implementation &#39;androidx.appcompat:appcompat:1.6.1&#39; implementation &#39;com.google.android.material:material:1.8.0&#39; compileOnly &#39;org.apache.httpcomponents:httpclient:4.5.13&#39; annotationProcessor &#39;com.google.dagger:dagger-compiler:2.44&#39; testImplementation &#39;junit:junit:4.13.2&#39; androidTestImplementation &#39;androidx.test.ext:junit:1.1.5&#39; androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.5.1&#39; } ``` ### 三、远程仓库配置 在 `build.gradle` 中还需要指定依赖项的来源,常见仓库包括: - `google()`:Google 的官方 Maven 仓库。 - `mavenCentral()`:Maven Central 仓库。 - `jcenter()`:JCenter 已停止服务,建议迁移至 Maven Central 或其他仓库。 这些配置通常位于模块级或项目级的 `repositories` 块中。 ### 四、镜像配置(可选) 为了加速依赖下载,可以在 `settings.gradle` 中配置镜像源,例如使用阿里云镜像: ```groovy pluginManagement { repositories { maven { url &#39;https://maven.aliyun.com/repository/public&#39; } google() mavenCentral() } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { maven { url &#39;https://maven.aliyun.com/repository/public&#39; } google() mavenCentral() } } ``` 这种方式可以全局替换默认的仓库地址,提升构建效率[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值