最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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打造屬于自己的新聞平臺(客戶端+服務(wù)器)

    Android打造屬于自己的新聞平臺(客戶端+服務(wù)器)

    這篇文章主要為大家詳細(xì)介紹了Android打造屬于自己的新聞平臺的相關(guān)資料,Android實現(xiàn)新聞客戶端服務(wù)器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android使用OpenGL和MediaCodec錄制功能

    Android使用OpenGL和MediaCodec錄制功能

    OpenGL是一個跨平臺的操作GPU的API,但OpenGL需要本地視窗系統(tǒng)進(jìn)行交互,這就需要一個中間控制層, EGL就是連接OpenGL ES和本地窗口系統(tǒng)的接口,引入EGL就是為了屏蔽不同平臺上的區(qū)別,這篇文章主要介紹了Android使用OpenGL和MediaCodec錄制功能,需要的朋友可以參考下
    2025-04-04
  • Android完整的前后端交互參考案例

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

    這篇文章主要介紹了Android完整的前后端交互參考案例,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Android使用EventBus多次接收消息

    Android使用EventBus多次接收消息

    這篇文章主要為大家詳細(xì)介紹了Android使用EventBus多次接收消息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android客戶端程序Gradle如何打包

    Android客戶端程序Gradle如何打包

    這篇文章主要介紹了Android客戶端程序Gradle如何打包 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android實現(xiàn)在TextView文字過長時省略部分或滾動顯示的方法

    Android實現(xiàn)在TextView文字過長時省略部分或滾動顯示的方法

    這篇文章主要介紹了Android實現(xiàn)在TextView文字過長時省略部分或滾動顯示的方法,結(jié)合實例形式分析了Android中TextView控件文字顯示及滾動效果相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android?Flutter繪制有趣的?loading加載動畫

    Android?Flutter繪制有趣的?loading加載動畫

    在網(wǎng)絡(luò)速度較慢的場景,一個有趣的加載會提高用戶的耐心和對?App?的好感。本篇我們利用Flutter?的?PathMetric來玩幾個有趣的?loading?效果,感興趣的可以動手嘗試一下
    2022-07-07
  • Android ActionBar使用教程

    Android ActionBar使用教程

    這篇文章主要為大家分享了Android ActionBar使用教程,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android?Studio實現(xiàn)音樂播放器的全過程(簡單易上手)

    Android?Studio實現(xiàn)音樂播放器的全過程(簡單易上手)

    這篇文章主要給大家介紹了關(guān)于Android?Studio實現(xiàn)音樂播放器的相關(guān)資料,文中通過實例代碼以及圖文介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • Android仿微信底部按鈕滑動變色

    Android仿微信底部按鈕滑動變色

    這篇文章主要介紹了Android仿微信底部按鈕滑動變色,使用Fragment為Tab頁的滑動操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03

最新評論

新乐市| 资讯 | 鹤峰县| 镇坪县| 高平市| 连江县| 遂溪县| 柳河县| 庄河市| 汝城县| 普宁市| 五大连池市| 望谟县| 墨竹工卡县| 洛隆县| 枣庄市| 宜宾市| 子洲县| 鄱阳县| 商水县| 岢岚县| 区。| 谷城县| 沁水县| 大新县| 宜兰市| 桦南县| 瓦房店市| 阜康市| 广平县| 桦南县| 邵阳市| 昭觉县| 东兴市| 中西区| 星座| 赤峰市| 吕梁市| 安丘市| 晋江市| 长顺县|