Android App中使用LinearLayout進(jì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)這樣的布局,兩個按鈕居中,該怎樣做?
比較容易實現(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é)果
如果改成水平方向的布局
<?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",當(dāng)然如果想一探究竟,為啥不起作用,可以從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)文章
關(guān)于androidstuio導(dǎo)入系統(tǒng)源碼的問題
小編最近在做系統(tǒng)源碼導(dǎo)出來的小項目,在導(dǎo)入androidstudio過程中遇到過一些問題,本文以Schedule power on off為例給大家詳細(xì)介紹,需要的朋友參考下吧2021-06-06Android?ViewPager你可能不知道的刷新操作分享
這篇文章主要為大家詳細(xì)介紹了Android中ViewPager你可能不知道的刷新操作,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,需要的可以參考一下2023-05-05Android Studio 運行時出現(xiàn)的警告信息解決辦法
這篇文章主要介紹了Android Studio 運行時出現(xiàn)的警告信息解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06Android中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