如何用两个button等分屏幕宽度?

简介: 如何用两个button等分屏幕宽度?

如何用两个button等分屏幕宽度?

问题引入

现有一个小问题:如何使用两个按钮,然后将屏幕宽度评分?如图:
实现最终效果图
再进一步细节:如果按钮的宽度相等呢?不相等呢?

看看人家的实现
Android的实现

1.按钮宽度相等的实现
这里直接上布局文件(.xml)的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="忘记密码"
        />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"/>
</LinearLayout>

效果如下:
按钮等宽平分
Android里面使用LinearLayout布局,然后直接使用了三个view,然偶设置其不可见(visibility="invisible"),但是占用当前的位置。最后设置每个layout里面的控件的weight为1,这样就保证了用两个按钮将屏幕等分。
2.按钮宽度不相等的实现:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册"
        android:minWidth="0dp"
        />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="忘记密码"
        android:minWidth="0dp"
        />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="invisible"/>
</LinearLayout>

效果如下:
android unequal
这里的代码和上面的大同小异,唯一的区别就是button设置了minWidth。

CSS+HTML的实现:
    <style type="text/css">
        .container {
            display: -webkit-box;
        }
        .container .tempDiv {
            -webkit-box-flex:1;
        }
    </style>

        <section class="container">
            <div class="tempDiv"></div>
            <button class="btn_login">登录</button>
            <div class="tempDiv"></div>
            <button class="btn_forget">忘记密码</button>
            <div class="tempDiv"></div>
        </section>

运行效果如下:
html 实现
html这边比较简单,只是把关键代码列出来了。直接使用-webkit-box来设置,然后将每个tempDiv的-webkit-box-flex都设置为1即可。
因为比较简单,所以也没有分按钮width是否相等两种情况。

iOS中的实现

iOS实现如果单独做计算也是可以的。可以先将按钮的宽度相加,然后(屏幕宽度-按钮相加宽度)/3 得到间隙的大小,然后再将按钮按照计算结果进行计算放置即可。这里需要固定按钮的宽度,才能实现。这里有个精度损失问题,因为在Int和Float之间转换有损失。关键代码如下:

         let interval = (screenWidth - CGFloat(forgetButtonWidth)  - CGFloat(loginButtonWidth) )/3
        
        loginButton.setTitle("登录", for: UIControlState.normal)
        loginButton.setTitleColor(UIColor.white, for: UIControlState.normal)
        loginButton.backgroundColor = UIColor.brown
        loginButton.frame = CGRect.init(x: Int(interval), y: 100, width: loginButtonWidth, height: 40)
        
        forgetPwdButton.setTitle("忘记密码", for: UIControlState.normal)
        forgetPwdButton.setTitleColor(UIColor.white, for: UIControlState.normal)
        forgetPwdButton.backgroundColor = UIColor.darkGray
        forgetPwdButton.frame = CGRect.init(x: Int(interval)*2+loginButtonWidth, y: 100, width: forgetButtonWidth, height: 40)
      
        self.view.addSubview(loginButton)
        
        self.view.addSubview(forgetPwdButton)

严格来说,这里并没有将其平分为三份,不过如果不是太严谨的话可以当做平分。
上面的这种通过计算的方式适合于按钮等宽或者不等宽的情况进行平分。接下来看看另一种方式:Stack view分割。
先简单说一下Stack view。它在IOS中的体现就是UIStackView这个类,是iOS9新增的一个布局技术,可以用来进行多个控件的快速布局。具体的介绍就不多说了,不了解的可以参考这里。
通过StackView,可以快速地实现分割,关键代码如下:

          loginButton.setTitle("登录", for: UIControlState.normal)
    
            loginButton.setTitleColor(UIColor.white, for: UIControlState.normal)
            loginButton.backgroundColor = UIColor.brown
    
            forgetPwdButton.setTitle("忘记密码", for: UIControlState.normal)
    
            forgetPwdButton.setTitleColor(UIColor.white, for: UIControlState.normal)
            forgetPwdButton.backgroundColor = UIColor.darkGray
    
    
            tempViewOne.backgroundColor = UIColor.clear
    
            tempViewTwo.backgroundColor = UIColor.clear
            tempViewThree.backgroundColor = UIColor.clear
    
            stackView.addArrangedSubview(tempViewOne)
            stackView.addArrangedSubview(forgetPwdButton)
            stackView.addArrangedSubview(tempViewTwo)
            stackView.addArrangedSubview(loginButton)
            stackView.addArrangedSubview(tempViewThree)
    //
            //set vertical or horizontal
            stackView.axis = UILayoutConstraintAxis.horizontal
        
            stackView.distribution = UIStackViewDistribution.fillEqually
            
            self.view.addSubview(stackView)

这里是用了三个临时的占位view,然后将他们和两个按钮都放到stackView里面,设置stackView的distribution为fillEqually。这样就实现了等分效果,这里需要注意:我们只需要对stack进行约束,然后其他的控件布局会由stack view自动管理。实现效果如下:
stack view handle
这样就实现了两个button把界面均分。

这里面设置的distribution是 fillEqually,也就是把stackview里面的控件都充满stackview,并且stackview的arranged view都是等宽的。

Question

1、还有其他的方式实现么?

相关文章
|
存储 关系型数据库 MySQL
DataX:导入4字节UTF8编码(生僻字)到Mysql数据库的utf8mb4数据表
MySql数据库的编码支持UFT8字符集。utf-8编码可能是2个字节、3个字节、4个字节的字符,MYSQL的utf-8编码,只支持3个字节的字符。汉字中很多生僻字都是4个字节的字符,日常生活中人的姓名就会有很多高位的生僻字。
7261 1
|
API Python
python 通过 github api 上传图片,实现免费的图床
python 通过 github api 上传图片,实现免费的图床
1593 0
|
C#
WPF中画虚线
原文:WPF中画虚线 在WPF中,画线的方法十分简单,只要声明一个Line然后添加到指定的位置就可以了,但Line并不仅仅只能画一条直线,还可以对直线进行修饰。
2351 0
|
Python
查找某文件夹下所有子文件夹内的py文件
import globimport os fp = '/Users/../Desktop'pylist = []def get_py(fp): filelist = glob.glob(os.path.
1729 0
|
监控 数据中心 UED
|
10天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
11784 32
|
10天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3008 16
|
5天前
|
人工智能 安全 应用服务中间件
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
NGINX曝出两个高危漏洞(CVE-2026-123633和CVE-2026-123007),CVSS 9.2分,均属HTTP/2内存安全缺陷,无需认证即可远程执行代码。影响自2015年起的多个版本,静态站点亦受威胁。修复版本为开源版1.30.7/1.31.9。(239字)
1514 3
NGINX 爆出两处高危漏洞 可导致未经身份验证的远程代码执行
|
8天前
|
传感器 安全 区块链
Windows 电脑运行安卓应用教程:开源工具 ApkStart 双击 APK 即装、独立窗口运行
ApkStart 是一款开源Windows工具,让安卓APP像原生软件一样运行:双击APK即安装、生成真实图标、独立窗口启动、开始菜单可搜索。无需传统模拟器,内置QEMU运行时,支持数据互通、ARM兼容与命令行操作。(239字)
|
2天前
|
存储 安全 网络协议
模型列表里看不到新上线的模型?分清内置目录、端点探测与能力声明
在 DSH 中配置模型提供商时,最容易混淆的是三类不同的「模型信息来源」:**已安装目录**、**端点实时探测**和**使用者自行声明的能力**。内置提供商的探测结果来自已安装目录,即使 API 地址指向网关,也不会变成网关的实时列表;列表中出现某个模型 ID,也不代表端点确实支持相应的上下文窗口或图片输入。本文依次梳理三个配置路径、列表看似过时的原因、探测与真实请求的区别、凭据和网络的排查方式,以及安全边界。
447 95

热门文章

最新文章