AndroidStudio界面布局

用惯了VisualStudio,这AndroidStudio真是不习惯。个人感觉VisualStudio是一个严格按照面向对象思想制作的集成化、所见即所得的开发工具。而AndroidStudio则是一个大杂烩。

就界面布局来说,VS的布局是由容器+控件的位置属性来构成,是面向对象的。使用简洁明了。AndroidStudio的布局就是想啥来啥,虽然也有可视化的设计器,但设计器里托拽下的位置居然仅供设计参考用,运行时无效。必需专门给控件添加布局参数。

AndroidStudio默认布局方式为ConstraintLayout布局,我认为引导线定位是一个兼容性较好的定位方式。

XML 复制代码
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 垂直 Guideline,距左侧 100dp -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="100dp" />
        
    <!-- 水平引导线:居中 -->
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/h1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />
        
    
    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintWidth_percent="1.0"
        app:layout_constraintHeight_percent="0.25"
        app:layout_constraintBottom_toTopOf="@id/h1" />
    
    
      <!-- 水平居中 -->
      <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintEnd_toEndOf="parent"/>
        
      <!-- 垂直居中 --> 
      <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击我"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/> 

</androidx.constraintlayout.widget.ConstraintLayout>
相关推荐
执明wa3 小时前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
JMchen3 小时前
Android自定义View三大核心流程:measure、layout、draw完全解密
android·android studio
古法安卓1 天前
Android-Fork 机制详解
android·java·android studio
JMchen1 天前
第 12 篇|项目整合与打包发布 —— 从 Demo 到可安装 APK 的完整收官指南
kotlin·android studio
JMchen2 天前
第 9 篇|网络请求基础 —— 从 HttpURLConnection 到 OkHttp
kotlin·android studio
程序员老刘3 天前
Android Studio Quail 4发布,看日志我以为谷歌放弃Flutter了
flutter·android studio·ai编程
JMchen3 天前
第 8 篇|本地存储技术选型 —— SP 与文件读写
kotlin·android studio
挂科边缘5 天前
Android Studio 保姆级安装教程,Windows 电脑上安装 Android Studio
android·windows·android studio
Danny_姜5 天前
Android Studio Quail 4 稳定版发布:本地 Gemma 4 + Android Skills
android·ide·android studio
我命由我123457 天前
Android Compose 开发,使用 ConstraintLayout,但是引入的是旧的 ConstraintLayout
android·java·java-ee·android studio·android jetpack·android-studio·android runtime