如何使用 6 个按钮(如 Windows 磁贴)创建布局

2023-11-21

我正在尝试创建一个包含 6 个按钮的布局,可以像 Windows Phone 的平铺一样自动适应屏幕尺寸。在代码中,我动态创建了 6 个按钮,2 个用于行,但按钮应该适合填充后者的屏幕大小。我该如何继续?

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

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up" />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
        />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
        />

</LinearLayout>


我会用垂直的LinearLayout三行与孩子的重量相同,每行都是水平的LinearLayout有两个体重相同的孩子,这将确保整个区域被填满。对于六个按钮来说,性能应该不是问题。

如果性能是一个问题,您可以将行设置为RelativeLayout并使用支柱将其分成两半,并据此定位两个孩子。

当我说一个strut,我的意思是:

<View android:id="@+id/strut"
    android:layout_width="0dp"
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true"/>

Update:既然你正在尝试LinearLayouts,以下是处理高度和宽度的方法:

家长LinearLayout可以有:

android:layout_width="match_parent"
android:layout_height="match_parent"

他们三个LinearLayout孩子们将有:

android:layout_width="match_parent"
android:layout_height="0dip"

The Buttons 将有:

android:layout_width="0dip"
android:layout_height="match_parent"

正如您所注意到的,我们有0dip对于应用权重的属性(如果父级是垂直方向,则在高度上;如果父级是水平方向,则在高度上),该属性需要增长以填充空间。

以下是完整的 XML(按钮不包含可绘制对象,因此请随意添加您的):

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

And the result: enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 6 个按钮(如 Windows 磁贴)创建布局 的相关文章

随机推荐

  • 谷歌云消息安全

    公司创建一个项目并接收发件人 ID 公司创建一个应用程序 烘焙其发件人 ID 并将该应用程序放置在商店中 攻击者对应用程序进行逆向工程 并提取发送者 ID 和用于接收 GCM 注册 ID 的服务器接口 攻击者创建自己的应用程序 嵌入公司的发
  • 从本地范围返回值?

    在我们的代码库中遇到这样的代码 这让我很担心 int foo int a Forward declaration int baz int result int a dosomestuff foo a 0 1 return result 这段
  • 如何设置WCF测试客户端在每次服务运行时运行

    我有一个 WCF 项目 当我运行它时 有时会启动 WCF 测试 有时则不会 如何设置 WCF 测试客户端始终显示 MLF 答案中的链接确实包含很好的信息 绝对值得一读 然而 我花了一段时间才找到我正在寻找的具体答案 大约在页面的中间 如果在
  • 如何将 create-react-app 与 Spring Boot 一起使用?

    我正在尝试将 ReactJS 应用程序与使用 Spring Boot 构建的 API 一起提供 我已经运行了脚本create react app在 public 上 如下面的结构所示 但是 当我尝试从 ReactJS 访问它时 所有相对路径
  • 在单行中打印不带括号的列表

    我有一个Python列表 例如 names Sam Peter James Julian Ann 我想在一行中打印数组 而不使用普通的 names Sam Peter James Julian Ann print names 输出如下 Sa
  • 在反应中插入光标

    我需要在插入符 当前光标位置 处插入文本React 控制文本区域 如自动完成 对于普通文本区域 我使用了以下代码 insertAtCursor function myField myValue IE if document selectio
  • 如何将 AKSequencer 连接到 AKCallbackInstrument?

    我想读取 MIDI 文件并在触发音符事件时显示内容 我发现了这个 stackoverflow 问题 其中第二个答案建议使用 AudioKit 如何为我的音频应用程序获得可靠的计时 更准确地说 建议使用 AKSequencer 吸收应用程序中
  • ios 11 导航栏重叠状态栏

    在 ios 11 中 导航栏与状态栏重叠 如果有人遇到同样的问题 请提供帮助 不确定这是否是同样的问题 但我们在升级到 iOS 11 时也遇到了这个问题 See ios 11 自定义导航栏位于状态栏下方 我们手动将导航栏高度设置为 64 并
  • Spring MVC JavaConfig webapp 中的“错误页面”配置? (没有 web.xml)

    我将如何使用 Java 配置向 Spring MVC Web 应用程序添加 错误页面 类型配置 没有 web xml
  • Apache Flink - 如果 x 分钟内没有收到数据,则发送事件

    如何使用 Flink 的 DataStream API 实现一个运算符 以便在一段时间内没有从流中接收到数据时发送事件 这样的运算符可以使用ProcessFunction DataStream
  • 第一个实体框架 6 nonquery 的六秒预热时间

    从我的集成测试来看 Act Stopwatch w new Stopwatch w Start userService Create userDTO w Stop public void Create UserDTO userDTO var
  • 如何让我的 Chrome 扩展程序只注入一次脚本?

    我在用着程序化注入仅在单击浏览器操作时将我的扩展程序的代码注入页面 这就是我的扩展中的内容活动页面 根据中的示例文档 chrome browserAction onClicked addListener function callback
  • JQuery ajax 调用 httpget webmethod (c#) 不起作用

    我试图让 ajax 访问后面代码中的 webmethod 问题是我不断从 jQuery 收到错误 parserror onfail method 如果我将 GET 更改为 POST 一切正常 请参阅下面我的代码 阿贾克斯调用
  • 有reactive-banana-gtk吗?

    我找到了reactive banana wax 想知道是否还有一个类似的gtk包 因为reactive香蕉应该支持gtk 根据http www haskell org haskellwiki Reactive banana The reac
  • 用于诊断 .NET 中内存泄漏的工具(ASP.NET 应用程序)[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 我们需要一个工具来轻松检测 ASP NET 应用程序中的内存泄漏 我们有一个应用程序 它消耗大量内存 Thanks Using WinDbg不像使用
  • Thread.CurrentPrincipal.Identity vs HttpContext.User.Identity [重复]

    这个问题在这里已经有答案了 可能的重复 http context user 和 thread currentprincipal 之间的区别以及何时使用它们 这两者在 ASP NET 应用程序中有何区别 我知道HttpContext User
  • 如何将 CSS 用于 Vaadin 组件?

    我似乎看到了一些例子 人们回答了如何通过添加 CSS 代码从组件中获取某些特定行为的问题 但是似乎没有人解释如何使用该 CSS 代码将其连接到 Java 组件 v table body overflow hidden important 例
  • 一个目录中的多个 Git 存储库

    我想将一个目录部署给具有不同权限的多个开发人员 所以这是 Git 无法做到的一件事 如何在一个目录中创建两个存储库 并通过使用排除由另一个存储库管理的文件来为它们分配不同的文件列表 gitignore file 例子 www project
  • MySQL“NOT IN”查询3个表

    我有 3 个表格课程 成绩和评估 我想比较两个表的等级和评价 如果表等级中的数据在表评价中不存在 则该数据会出现 输出 select Grade ID Courses Course ID Courses Grade NAME Course
  • 如何使用 6 个按钮(如 Windows 磁贴)创建布局

    我正在尝试创建一个包含 6 个按钮的布局 可以像 Windows Phone 的平铺一样自动适应屏幕尺寸 在代码中 我动态创建了 6 个按钮 2 个用于行 但按钮应该适合填充后者的屏幕大小 我该如何继续