`

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>
  • 大小: 25 KB
  • 大小: 25 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics