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>
相关推荐
我命由我123451 天前
Android 开发,FragmentPagerAdapter 的 isViewFromObject 方法问题
android·java-ee·kotlin·android studio·android jetpack·android-studio·android runtime
帅次1 天前
Kotlin MVVM 实战入门:从分层到状态闭环
android·kotlin·android studio·android jetpack
问心无愧05132 天前
ctf show web入门100
android·ide·笔记·android studio
我命由我123453 天前
Android Service - Service 生命周期变化、Service 与 Activity 双向交互
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
执明wa4 天前
Android Studio 项目目录结构全方位详解
android·ide·android studio
我命由我123454 天前
Android 开发问题:Could not find com.github.PicnicSupermarket:FingerPaintView:1.2.
android·github·android studio·安卓·android jetpack·android-studio·android runtime
曼岛_5 天前
[安卓逆向]在Android Studio中编写SO文件并测试调用 (四)
android·ide·android studio
眸生5 天前
基于NeteaseCloudMusicApi的音乐app 支持 DeepSeek 自然语言找歌、批量导入歌单、下载音乐转换成MP3,下载歌词
android·python·kotlin·android studio·音频·fastapi·android jetpack
帅次8 天前
Android 17 开发者实战:核心更新与应用场景落地指南
android·java·ios·android studio·iphone·android jetpack·webview
独隅8 天前
Android Studio 接入多种不同 AI 大模型进行开发的全面详细指南(Android Studio+AI)
android·人工智能·android studio