Android App中使用LinearLayout進行居中布局的實例講解
要想讓您的控件水平居中或垂直居中其實很簡單,只要在控件的上一級中設(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)這樣的布局,兩個按鈕居中,該怎樣做?

比較容易實現(xiàn)的是LinearLayout布局,當然如果換成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標簽刪除即可,但是有些人這么去實現(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標簽指定了android:layout_gravity="center"但是貌似布局不領(lǐng)情,為啥?
原因很簡單,LinearLayout只能有一個方向,要么Vertical,要么Horizontal,完全不會領(lǐng)layout_gravity這個屬性的情,所以上面的實現(xiàn)只有這么一個結(jié)果

如果改成水平方向的布局
<?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é)果

按照上面的理論也就能想通了,所以android:layout_gravity="center"這個屬性雖然LinearLayout也具有,但是卻虛晃一槍,真正其作用的還是其本身的屬性android:gravity="center",當然如果想一探究竟,為啥不起作用,可以從LinearLayout這個布局的源代碼看起,這里不再贅述。
- Android使用LinearLayout設(shè)置邊框
- Android應(yīng)用借助LinearLayout實現(xiàn)垂直水平居中布局
- android LinearLayout和RelativeLayout組合實現(xiàn)精確布局方法介紹
- Android LinearLayout實現(xiàn)自動換行效果
- Android中LinearLayout布局的常用屬性總結(jié)
- Android自定義LinearLayout布局顯示不完整的解決方法
- Android App中的多個LinearLayout嵌套布局實例解析
- Android自定義控件LinearLayout實例講解
- Android?LinearLayout快速設(shè)置每個item間隔
相關(guān)文章
Android打造屬于自己的新聞平臺(客戶端+服務(wù)器)
這篇文章主要為大家詳細介紹了Android打造屬于自己的新聞平臺的相關(guān)資料,Android實現(xiàn)新聞客戶端服務(wù)器,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06
Android使用OpenGL和MediaCodec錄制功能
OpenGL是一個跨平臺的操作GPU的API,但OpenGL需要本地視窗系統(tǒng)進行交互,這就需要一個中間控制層, EGL就是連接OpenGL ES和本地窗口系統(tǒng)的接口,引入EGL就是為了屏蔽不同平臺上的區(qū)別,這篇文章主要介紹了Android使用OpenGL和MediaCodec錄制功能,需要的朋友可以參考下2025-04-04
Android實現(xiàn)在TextView文字過長時省略部分或滾動顯示的方法
這篇文章主要介紹了Android實現(xiàn)在TextView文字過長時省略部分或滾動顯示的方法,結(jié)合實例形式分析了Android中TextView控件文字顯示及滾動效果相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android?Flutter繪制有趣的?loading加載動畫
在網(wǎng)絡(luò)速度較慢的場景,一個有趣的加載會提高用戶的耐心和對?App?的好感。本篇我們利用Flutter?的?PathMetric來玩幾個有趣的?loading?效果,感興趣的可以動手嘗試一下2022-07-07
Android?Studio實現(xiàn)音樂播放器的全過程(簡單易上手)
這篇文章主要給大家介紹了關(guān)于Android?Studio實現(xiàn)音樂播放器的相關(guān)資料,文中通過實例代碼以及圖文介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友可以參考下2022-02-02

