码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-04-01 20:27:36      阅读:611      评论:0      收藏:0      [点我收藏+]

标签:

问题描述:

如何使按钮水平垂直居中且始终占据屏幕宽度一半

效果如下:

竖屏:

技术分享

 

横屏:

技术分享

 

解决方案:

使用线性布局,指定线性布局的总权重(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>

 

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

标签:

原文地址:http://www.cnblogs.com/davidgu/p/5345978.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!