[b]问题描述:[/b]
如何使按钮水平垂直居中且始终占据屏幕宽度一半
效果如下:
竖屏:
[img]http://dl2.iteye.com/upload/attachment/0116/2834/595bbdc6-b0ba-371b-9faf-78cdcf010460.png[/img]
横屏:
[img]http://dl2.iteye.com/upload/attachment/0116/2836/84dac890-2ee3-3889-90ec-f7f99a61bca5.png[/img]
[b]解决方案:[/b]
使用线性布局,指定线性布局的总权重(weightSum)为1, 指定按钮的权重为其一半即0.5
布局代码如下:
如何使按钮水平垂直居中且始终占据屏幕宽度一半
效果如下:
竖屏:
[img]http://dl2.iteye.com/upload/attachment/0116/2834/595bbdc6-b0ba-371b-9faf-78cdcf010460.png[/img]
横屏:
[img]http://dl2.iteye.com/upload/attachment/0116/2836/84dac890-2ee3-3889-90ec-f7f99a61bca5.png[/img]
[b]解决方案:[/b]
使用线性布局,指定线性布局的总权重(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>
本文介绍了一种利用线性布局实现按钮在不同屏幕方向下(竖屏和横屏)水平垂直居中的方法,并确保按钮始终占据屏幕宽度的一半。通过设置线性布局的总权重为1,按钮权重为0.5来达成这一效果。
8540

被折叠的 条评论
为什么被折叠?



