欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android App中使用LinearLayout進(jìn)行居中布局的實例講解

 更新時間:2016年04月13日 15:13:04   作者:linux1s1s  
這篇文章主要介紹了Android App中使用LinearLayout進(jìn)行居中布局的實例講解,文中分別介紹了水平居中和垂直居中的相關(guān)線性布局,需要的朋友可以參考下

要想讓您的控件水平居中或垂直居中其實很簡單,只要在控件的上一級中設(shè)置【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控件就乖乖的待在你選定區(qū)域的正中間了。 gravity的屬性值還有很多,可以單獨設(shè)置水平或垂直居中。大家可以查看相應(yīng)的文檔,或使用Eclipse的提示功能快速查看。
   
我們來看一個實例,如果你想實現(xiàn)這樣的布局,兩個按鈕居中,該怎樣做?

2016413150749150.jpg (386×727)

比較容易實現(xiàn)的是LinearLayout布局,當(dāng)然如果換成RelativeLayout同樣也可以實現(xiàn)。
這里簡單說一下用RelativeLayout布局如何實現(xiàn),首先需找中心點,然后以這個中心點為參照物即可實現(xiàn)。接下來看一下更容易實現(xiàn)的LinearLayout布局的實現(xiàn)。

<?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>

如果現(xiàn)在需求改變了,中間只需要一個Button即可,那么實現(xiàn)起來很簡單,只需要將上面的其中一個Button標(biāo)簽刪除即可,但是有些人這么去實現(xiàn)

<?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>

這么實現(xiàn)顯然不能居中,雖然在Button標(biāo)簽指定了android:layout_gravity="center"但是貌似布局不領(lǐng)情,為啥?
原因很簡單,LinearLayout只能有一個方向,要么Vertical,要么Horizontal,完全不會領(lǐng)layout_gravity這個屬性的情,所以上面的實現(xiàn)只有這么一個結(jié)果

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>

會變成這樣的結(jié)果

2016413150854909.jpg (370×723)

按照上面的理論也就能想通了,所以android:layout_gravity="center"這個屬性雖然LinearLayout也具有,但是卻虛晃一槍,真正其作用的還是其本身的屬性android:gravity="center",當(dāng)然如果想一探究竟,為啥不起作用,可以從LinearLayout這個布局的源代碼看起,這里不再贅述。

相關(guān)文章

  • Android中內(nèi)存泄漏需要的注意點

    Android中內(nèi)存泄漏需要的注意點

    在本篇文章里小編給大家整理了關(guān)于Android中內(nèi)存泄漏需要的注意點的相關(guān)內(nèi)容,有此需要的朋友們參考下。
    2019-06-06
  • 關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題

    關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題

    小編最近在做系統(tǒng)源碼導(dǎo)出來的小項目,在導(dǎo)入androidstudio過程中遇到過一些問題,本文以Schedule power on off為例給大家詳細(xì)介紹,需要的朋友參考下吧
    2021-06-06
  • 分享Android中pullToRefresh的使用心得

    分享Android中pullToRefresh的使用心得

    這篇文章主要介紹了分享Android中pullToRefresh的使用心得的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Android Canvas繪制文字橫縱向?qū)R

    Android Canvas繪制文字橫縱向?qū)R

    這篇文章主要介紹了Android Canvas繪制文字橫縱向?qū)R,Align屬性決定了使用該畫筆時,相較于繪制點的水平對稱方式,分別是LEFT、CENTER、RIGHT,更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容
    2022-06-06
  • Android?ViewPager你可能不知道的刷新操作分享

    Android?ViewPager你可能不知道的刷新操作分享

    這篇文章主要為大家詳細(xì)介紹了Android中ViewPager你可能不知道的刷新操作,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的可以參考一下
    2023-05-05
  • Kotlin Fragment使用方法詳解

    Kotlin Fragment使用方法詳解

    Fragment是Android3.0后引入的一個新的API,他出現(xiàn)的初衷是為了適應(yīng)大屏幕的平板電腦, 當(dāng)然現(xiàn)在他仍然是平板APP UI設(shè)計的寵兒,而且我們普通手機(jī)開發(fā)也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段
    2023-01-01
  • Android OKHttp使用簡介

    Android OKHttp使用簡介

    目前Android端調(diào)用網(wǎng)絡(luò)請求最常用的框架就是OKHttp,目前項目中也經(jīng)常會用到。介紹下OKHttp的使用場景
    2021-05-05
  • Android Studio 運行時出現(xiàn)的警告信息解決辦法

    Android Studio 運行時出現(xiàn)的警告信息解決辦法

    這篇文章主要介紹了Android Studio 運行時出現(xiàn)的警告信息解決辦法的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Android中View.post和Handler.post的關(guān)系

    Android中View.post和Handler.post的關(guān)系

    這篇文章主要介紹了Android中View.post和Handler.post的關(guān)系,View.post和Handler.post是Android開發(fā)中經(jīng)常使用到的兩個”post“方法,關(guān)于兩者存在的區(qū)別與聯(lián)系,文章詳細(xì)分析需要的小伙伴可以參考一下
    2022-06-06
  • Android利用OpenGLES繪制天空盒實例教程

    Android利用OpenGLES繪制天空盒實例教程

    這篇文章主要給大家介紹了關(guān)于Android利用OpenGLES繪制天空盒的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08

最新評論