Android App中使用LinearLayout进行居中布局的实例讲解

 更新时间:2016年04月13日 15:13:04   作者:linux1s1s  
这篇文章主要介绍了Android App中使用LinearLayout进行居中布局的实例讲解,文中分别介绍了水平居中和垂直居中的相关线性布局,需要的朋友可以参考下

要想让您的控件水平居中或垂直居中其实很简单,只要在控件的上一级中设置【android:gravity="center"】属性即可
如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:gravity="center"
  android:background="#000000"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <ImageView
  android:id="@+id/logo"
  android:src="@drawable/logo"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
</LinearLayout> 

这样一个ImageView控件就乖乖的待在你选定区域的正中间了。 gravity的属性值还有很多,可以单独设置水平或垂直居中。大家可以查看相应的文档,或使用Eclipse的提示功能快速查看。
   
我们来看一个实例,如果你想实现这样的布局,两个按钮居中,该怎样做?

2016413150749150.jpg (386×727)

比较容易实现的是LinearLayout布局,当然如果换成RelativeLayout同样也可以实现。
这里简单说一下用RelativeLayout布局如何实现,首先需找中心点,然后以这个中心点为参照物即可实现。接下来看一下更容易实现的LinearLayout布局的实现。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center">
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/start_server"
      android:text="Start" />
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/stop_server"
      android:text="Stop" />
</LinearLayout>

如果现在需求改变了,中间只需要一个Button即可,那么实现起来很简单,只需要将上面的其中一个Button标签删除即可,但是有些人这么去实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/start_server"
      android:layout_gravity="center"
      android:text="Start" />
</LinearLayout>

这么实现显然不能居中,虽然在Button标签指定了android:layout_gravity="center"但是貌似布局不领情,为啥?
原因很简单,LinearLayout只能有一个方向,要么Vertical,要么Horizontal,完全不会领layout_gravity这个属性的情,所以上面的实现只有这么一个结果

2016413150836754.jpg (374×720)

如果改成水平方向的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
  <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/start_server"
      android:layout_gravity="center"
      android:text="Start" />
</LinearLayout>

会变成这样的结果

2016413150854909.jpg (370×723)

按照上面的理论也就能想通了,所以android:layout_gravity="center"这个属性虽然LinearLayout也具有,但是却虚晃一枪,真正其作用的还是其本身的属性android:gravity="center",当然如果想一探究竟,为啥不起作用,可以从LinearLayout这个布局的源代码看起,这里不再赘述。

相关文章

  • Android打造属于自己的新闻平台(客户端+服务器)

    Android打造属于自己的新闻平台(客户端+服务器)

    这篇文章主要为大家详细介绍了Android打造属于自己的新闻平台的相关资料,Android实现新闻客户端服务器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android使用OpenGL和MediaCodec录制功能

    Android使用OpenGL和MediaCodec录制功能

    OpenGL是一个跨平台的操作GPU的API,但OpenGL需要本地视窗系统进行交互,这就需要一个中间控制层, EGL就是连接OpenGL ES和本地窗口系统的接口,引入EGL就是为了屏蔽不同平台上的区别,这篇文章主要介绍了Android使用OpenGL和MediaCodec录制功能,需要的朋友可以参考下
    2025-04-04
  • Android完整的前后端交互参考案例

    Android完整的前后端交互参考案例

    这篇文章主要介绍了Android完整的前后端交互参考案例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Android使用EventBus多次接收消息

    Android使用EventBus多次接收消息

    这篇文章主要为大家详细介绍了Android使用EventBus多次接收消息,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android客户端程序Gradle如何打包

    Android客户端程序Gradle如何打包

    这篇文章主要介绍了Android客户端程序Gradle如何打包 的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android实现在TextView文字过长时省略部分或滚动显示的方法

    Android实现在TextView文字过长时省略部分或滚动显示的方法

    这篇文章主要介绍了Android实现在TextView文字过长时省略部分或滚动显示的方法,结合实例形式分析了Android中TextView控件文字显示及滚动效果相关操作技巧,需要的朋友可以参考下
    2016-10-10
  • Android Flutter绘制有趣的 loading加载动画

    Android Flutter绘制有趣的 loading加载动画

    在网络速度较慢的场景,一个有趣的加载会提高用户的耐心和对 App 的好感。本篇我们利用Flutter 的 PathMetric来玩几个有趣的 loading 效果,感兴趣的可以动手尝试一下
    2022-07-07
  • Android ActionBar使用教程

    Android ActionBar使用教程

    这篇文章主要为大家分享了Android ActionBar使用教程,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android Studio实现音乐播放器的全过程(简单易上手)

    Android Studio实现音乐播放器的全过程(简单易上手)

    这篇文章主要给大家介绍了关于Android Studio实现音乐播放器的相关资料,文中通过实例代码以及图文介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友可以参考下
    2022-02-02
  • Android仿微信底部按钮滑动变色

    Android仿微信底部按钮滑动变色

    这篇文章主要介绍了Android仿微信底部按钮滑动变色,使用Fragment为Tab页的滑动操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03

最新评论