Android应用借助LinearLayout实现垂直水平居中布局

 更新时间:2016年04月13日 14:46:05   作者:andie_guo  
这篇文章主要介绍了Android应用借助LinearLayout实现垂直水平居中布局的方法,文中列举了LinearLayout线性布局下居中相关的几个重要参数,需要的朋友可以参考下

首先说的是LinearLayout布局下的居中一般是这样的:
(注意:android:layout_width="fill_parent" android:layout_height="fill_parent" 属性中,若水平居中,至少在宽度上占全屏;若垂直居中,则在高度上占全屏)

<LinearLayout 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:gravity="center|center_horizontal|center_vertical" > 
  // 上面gravity属性的参数:center为居中,center_horizontal为水平居中,center_vertical为垂直居中 
  <Button  
    android:id="@+id/Binding_button"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="关联新账户" /> 
</LinearLayout>  

要实现垂直水平居中,几个基本参数值得注意:
1、android:orientation="vertical"表示该布局下的元素垂直排列;
2、android:layout_gravity="center_horizontal"表示该布局在父布局里水平居中,此时其父布局必须拥有android:orientation="vertical"属性;
3、android:layout_gravity="center_vertical"表示该布局在父布局里垂直居中,此时其父布局必须应设置成android:orientation="horizontal"属性(默认为该属性),且其父布局的高度应设置为android:layout_height="fill_parent"属性;
4、android:gravity="center_horizontal"表示该布局下的元素水平居中;

线性布局垂直水平居中布局文件实例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal" 
    > 
 
    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_gravity="center_vertical" 
      > 
 
      <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
 
        <TextView 
          android:layout_width="100dp" 
          android:layout_height="wrap_content" 
          android:gravity="right" 
          android:text="用户名" /> 
 
        <EditText 
          android:layout_width="300dp" 
          android:layout_height="wrap_content" /> 
      </LinearLayout> 
 
      <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" > 
 
        <TextView 
          android:layout_width="100dp" 
          android:layout_height="wrap_content" 
          android:gravity="right" 
          android:text="Email" /> 
 
        <EditText 
          android:layout_width="300dp" 
          android:layout_height="wrap_content" /> 
      </LinearLayout> 
    </LinearLayout> 
  </LinearLayout> 
</LinearLayout> 

相关文章

  • android中图片加载到内存的实例代码

    android中图片加载到内存的实例代码

    这篇文章主要介绍了android中图片加载到内存的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • Libgdx解决部分Android机型锁屏崩溃的方法

    Libgdx解决部分Android机型锁屏崩溃的方法

    今天小编就为大家分享一篇关于Libgdx解决部分Android机型锁屏崩溃的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • android自定义进度条渐变色View的实例代码

    android自定义进度条渐变色View的实例代码

    这篇文章主要介绍了android自定义进度条渐变色View的实例代码,有需要的朋友可以参考一下
    2014-01-01
  • 详解Android 获取手机中微信聊天记录方法

    详解Android 获取手机中微信聊天记录方法

    本篇文章给大家分享了Android 获取手机中微信聊天记录的详细步骤和操作方法,有需要的朋友可以学习参考下。
    2018-05-05
  • Android入门之使用SimpleAdapter实现复杂界面布局

    Android入门之使用SimpleAdapter实现复杂界面布局

    这篇文章主要为大家详细介绍了Android如何使用SimpleAdapter实现复杂的界面布局,文中的示例代码讲解详细,对我们学习Android有一定的帮助,需要的可以参考一下
    2022-11-11
  • Android内置的OkHttp用法介绍

    Android内置的OkHttp用法介绍

    okhttp是一个第三方类库,用于android中请求网络。这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient
    2022-08-08
  • react native打包apk文件安装好之后进入应用闪退的解决方案

    react native打包apk文件安装好之后进入应用闪退的解决方案

    这篇文章主要介绍了react native打包apk文件安装好之后进入应用闪退的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • Android仿一号店货物详情轮播图动画效果

    Android仿一号店货物详情轮播图动画效果

    这篇文章主要为大家详细介绍了Android-仿一号店货物详情轮播图动画效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Android实现动画效果详解

    Android实现动画效果详解

    这篇文章主要介绍了Android实现动画效果详解,目前Android平台提供了Tween动画和Frame动画,实现这两类动画有两种方式:一种使用XML文件(文件放在res/anim),一种直接代码搞定,需要的朋友可以参考下
    2015-07-07
  • Android View 事件防抖的两种方案

    Android View 事件防抖的两种方案

    这篇文章主要介绍了Android View 事件防抖的两种方案,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-04-04

最新评论