Android手機(jī)開(kāi)發(fā) 使用線(xiàn)性布局和相對(duì)布局實(shí)現(xiàn)Button垂直水平居中
居中呢,這里分兩種不同布局方式的居中!分別是 LinearLayout 和RelativeLayout。
一、首先說(shuō)的是LinearLayout布局下的居中:
注意:android:layout_width="fill_parent" android:layout_height="fill_parent" 屬性中,若水平居中,至少在寬度上占全屏;若垂直居中,則在高度上占全屏
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|center_horizontal|center_vertical" >
// 上面gravity屬性的參數(shù):center為居中,center_horizontal為水平居中,center_vertical為垂直居中
<Button
android:id="@+id/Binding_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="關(guān)聯(lián)新賬戶(hù)" />
</LinearLayout>
二、然后說(shuō)的是RelativeLayout布局下的居中:
<RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
Android:layout_width="fill_parent" Android:layout_height="fill_parent">
<Button Android:id="@+id/btngal" Android:layout_width="wrap_content"
Android:layout_height="wrap_content" Android:gravity="center_horizontal"
Android:textSize="20sp" Android:layout_alignParentBottom="true"
Android:layout_centerHorizontal="true" Android:text="返回主界面" />
</RelativeLayout>
簡(jiǎn)單說(shuō)明
Android:gravity="CENTER_VERTICAL“:這個(gè)是垂直居中對(duì)齊
Android:gravity="BOTTOM”:放在容器的底部
Android:gravity="CENTER“ :放在容器的中心
三、一行居中兩個(gè)按鈕的方法
方法1:
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" style="@android:style/ButtonBar"> <Button android:id="@+id/btn_listview" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="ListView"/> <Button android:id="@+id/btn_emptyview" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight="1" android:text="EmptyView"/> </LinearLayout>
方法2:
<RelativeLayout android:id="@+id/relativeTop"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<Button
android:id="@+id/btnGetMp3s"
android:text="@string/strGetMp3List"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_toLeftOf="@id/txtHide" />
<TextView android:id="@+id/txtHide" android:layout_width="25" android:layout_height="1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btnExitSys"
android:text="@string/strExitSys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/txtHide" />
</RelativeLayout>
相關(guān)文章
GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例
這篇文章主要為大家詳細(xì)介紹了GridView實(shí)現(xiàn)桌面圖標(biāo)顯示案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android ImageButton自定義按鈕的按下效果的代碼實(shí)現(xiàn)方法分享
這篇文章主要介紹了Android ImageButton自定義按鈕的按下效果的代碼實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
Android編程實(shí)現(xiàn)項(xiàng)目中異常捕獲及對(duì)應(yīng)Log日志文件保存功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)項(xiàng)目中異常捕獲及對(duì)應(yīng)Log日志文件保存功能,涉及Android異常處理、日志讀寫(xiě)及權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
超實(shí)用的android自定義log日志輸出工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了一個(gè)超實(shí)用的android自定義log日志輸出工具類(lèi),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
AndroidManifest.xml uses-feature功能詳解
這篇文章主要介紹了AndroidManifest.xml uses-feature功能,較為詳細(xì)的分析了Android屬性過(guò)濾操作的功能與相關(guān)技巧,需要的朋友可以參考下2016-10-10
Android貝塞爾曲線(xiàn)實(shí)現(xiàn)填充不規(guī)則圖形并隨手指運(yùn)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android貝塞爾曲線(xiàn)實(shí)現(xiàn)填充不規(guī)則圖形,并隨手指運(yùn)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android RecyclerView仿新聞?lì)^條的頻道管理功能
這篇文章主要介紹了Android RecyclerView仿新聞?lì)^條的頻道管理功能,需要的朋友可以參考下2017-06-06

