Android开发点滴 - 如何使按钮水平垂直居中且始终占据屏幕宽度一半

问题描述:
如何使按钮水平垂直居中且始终占据屏幕宽度一半
效果如下:
竖屏:


横屏:


解决方案:
使用线性布局,指定线性布局的总权重(weightSum)为1, 指定按钮的权重为其一半即0.5
布局代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="#FFFFFF"
	android:gravity="center"
	android:orientation="horizontal"
	android:weightSum="1">
	<Button
		android:layout_width="0dp"
		android:layout_height="wrap_content"
		android:layout_weight="0.5"
		android:text="Click me"/>
</LinearLayout>

猜你喜欢

转载自kelvingjy.iteye.com/blog/2288095