行中的三个按钮 -> 调整大小以适合父级宽度

2024-04-01

我有这个布局

  • Linear Layout
    • Scroll View
      • Relative Layout
        • 9x 按钮

在这样的视图中(3x3 网格)

+---------+  
| o  o  o |  
| o  o  o |  
| o  o  o |
+---------+

每个按钮都有它的背景,没有文字,背景如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/menu_btn1_hover" android:state_pressed="true"/>
    <item android:drawable="@drawable/menu_btn1"/>
</selector>

我应该如何声明布局,以便按钮始终为每行 3 个,并调整大小以适合视图?


尝试这个!

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="match_parent">
        <Button android:id="@+id/Button04" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button>
        <Button android:id="@+id/Button05" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button>
        <Button android:id="@+id/Button06" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="match_parent">
        <Button android:id="@+id/Button01" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button>
        <Button android:id="@+id/Button02" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button>
        <Button android:id="@+id/Button03" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
        <Button android:id="@+id/button1" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button>
        <Button android:id="@+id/button2" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button>
        <Button android:id="@+id/button3" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button>
    </LinearLayout>

</LinearLayout>

截屏:

UPDATE:

如果您使用相同的layout_height和layout_width制作LinearLayout(android:id =“@+id/wrapper”(查看下面的代码)),您将得到您想要的

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="300dp" android:weightSum="1"
        android:orientation="vertical" android:layout_height="300dp">
        <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp">
            <Button android:id="@+id/Button04" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button>
            <Button android:id="@+id/Button05" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button>
            <Button android:id="@+id/Button06" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button>
        </LinearLayout>
        <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp">
            <Button android:id="@+id/Button01" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button>
            <Button android:id="@+id/Button02" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button>
            <Button android:id="@+id/Button03" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp">
            <Button android:id="@+id/button1" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button>
            <Button android:id="@+id/button2" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button>
            <Button android:id="@+id/button3" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

请查看屏幕截图。在第二个屏幕截图中,带有 android:id="@+id/wrapper" 的 LinearLayout 具有相同的宽度和高度,等于 300dp

请尝试一下!希望对你有帮助!!

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

行中的三个按钮 -> 调整大小以适合父级宽度 的相关文章

  • Android 中多个蓝牙连接的自定义 UUID

    我有一个 Android 设备作为服务器连接到多个蓝牙 Android 客户端 我了解 UUID 的概念以及它的独特之处 我的问题是 我可以为连接到我的服务器的所有客户端使用相同的 UUID 吗 如果没有 我如何以编程方式为我的客户端生成
  • 如何自定义菜单项的背景颜色?

    我正在尝试定制Toolbar的弹出菜单 现在我无法设置菜单项的背景颜色 我的 styles xml 如下所示
  • 如何获取每个StorageVolume的可用大小和总大小?

    背景 谷歌 悲伤 计划破坏存储权限 https www xda developers com android q storage access framework scoped storage 这样应用程序将无法使用标准文件 API 和文件
  • Cheesesquare:enterAlways 会产生错误的布局

    Adding enterAlways到 Cheesesquare 演示的滚动标志
  • Xamarin Android Webview Javascript

    我正在尝试通过 Xamarin for Android 创建一个移动应用程序 它有一个显示网站的 WebView 问题是正常按钮会触发 但 javascript 事件不会触发 我已经启用了 Javascript 但没有运气 如何在 Andr
  • fetchUuidsWithSdp 的奇怪 UUID 逆转

    我有一个在树莓派上运行的 python 蓝牙服务器 使用 PyBluez 我在服务器中使用的uuid是 8f86d132 4ab8 4c15 b8df 0b70cf10ea56 我正在打电话device fetchUuidsWithSdp
  • Recyclerview 动态部分不使用任何第三个库

    我想将标头添加到 recyclerview 我正在尝试使用来实现它 Override public int getItemViewType int position depends on your problem if position 0
  • Bitmap.getPixels() 中的 IllegalArgumentException

    我想将数据从位图复制到int using getPixels 这是我当前的代码 int pixels new int myBitmap getHeight myBitmap getWidth myBitmap getPixels pixel
  • 如何重定向到 instagram://user?username={username}

    我的 html 页面上有这个链接 可以在特定用户上打开 Instagram 应用程序 a href Link to Instagram Profile a 我一直在寻找自动运行 url instagram user username USE
  • ExoPlayer2 - 如何使 HTTP 301 重定向工作?

    我开始使用 ExoPlayer 来传输一些音频 一切都很顺利 直到我遇到一个带有 301 永久移动 重定向的 URL ExoPlayer2 默认情况下不处理该问题 我已经看过这个线程 https github com google ExoP
  • 从 BroadcastReceiver 类调用活动方法

    我知道我可以做一个内部接收器类来调用接收器中的任何方法 但我的主要活动太大了 要做的事情也很多 因此 我需要一个扩展广播接收器的类 但它不是内部类 并且可以从我的主要活动中调用一种方法 我不知道是否可能 但我的活动是家庭活动和 single
  • 对于一个单元格,RecyclerView onBindViewHolder 调用次数过多

    我正在将 RecyclerView 与 GridLayoutManager 一起使用 对于网格中的每个项目 我需要调用 REST api 来检索数据 然后 从远程异步获取数据后 我使用 UIL 加载 显示图像 一切似乎都很好 但我发现 on
  • Android 中如何通过彩信发送图片?

    我正在开发多媒体应用程序 我正在通过相机捕获一张图像 并希望将该图像和文本发送到其他号码 但我不知道如何通过彩信发送图像 MMS 只是一个 http post 请求 您应该使用执行请求额外的网络功能 final ConnectivityMa
  • WorkManager 或 AlarmManager 用于日常请求然后通知工作?

    这是用例 用户设置具有特定时间的每日通知 在指定时间 发出网络请求以获取一些数据 然后使用检索到的数据显示通知 我不确定是否应该使用 AlarmManager 还是 WorkManager 来实现这个用例 据我了解 AlarmManager
  • OnLongClickListener 不工作

    我有一个ImageView 我需要使用onLongClickListener对于图像视图 当我使用这段代码时 什么也没有发生 Code gallery Gallery findViewById R id gall1 gallery setA
  • 您使用什么物理 Android 设备进行测试?

    有什么好的推荐用于测试目的的物理 Android 设备吗 我正在苹果阵营寻找像 iPod touch 这样的设备 可以帮助 iOS 开发人员测试他们的东西 我知道有 Nexus One 但那东西相当昂贵 而且我并不真正关心手机的东西 而是可
  • 插件“Android Bundle Support”不兼容

    大家好 自从上次更新以来 当我启动 android studio 时 我遇到了一个非常奇怪的错误 我有这个错误 插件错误 插件 Android Bundle Support 不兼容 直到构建 AI 195 SNAPSHOT 我在网上找不到任
  • 没有支持 FEATURE_CAMERA_EXTERNAL 的 Android 设备

    根据this doc https source android com devices camera external usb cameras一些 Android 设备允许使用 Camera2 API 访问外部 USB 摄像头 我检查了大约
  • 在 Android 应用程序资源中使用 JSON 文件

    假设我的应用程序的原始资源文件夹中有一个包含 JSON 内容的文件 我如何将其读入应用程序 以便我可以解析 JSON See 开放原始资源 http developer android com reference android conte
  • Android 屏幕方向错误

    我使用的是 Android HTC HERO 2 1 版本 我写的活动

随机推荐

  • 让 CMake 将生成的二进制文件与资产一起放入特定的目录结构中

    我的项目的目录结构基本如下 root src 根 资产 根 库 我目前已将 CMake 设置为编译源代码 编译库 然后通过从根目录调用 make 来链接它们 然后 我必须手动将可执行文件移动到原始资产目 录中以使其运行 因为这是它期望的位置
  • params.getBehaviour() 返回空值

    我正在玩新的 Android 设计库 CollapsingToolbarLayout 工作得很好 但是 我无法将工具栏的默认状态设置为 折叠 我正在尝试实施所示的解决方案here https stackoverflow com questi
  • 无法确定表达式错误的序列化信息

    我得到一个Unable to determine the serialization information for the expression PlaceManager pm gt pm FreePlaces get Item inde
  • 在 Jetty WebAppContext 之间实现 SSO

    我正在开发的 Jetty 9 应用程序会自动扫描一组 JarFiles 中的 web xml 然后以编程方式将包含的 Web 应用程序导入为 WebAppContext 我需要在各个 Web 应用程序之间实现单点登录 如以下 Jetty 6
  • 在Java中编码base64并在C#中解码

    我在java和c 之间遇到了base64问题 我从java urlconnection发送编码字符串到asp net处理程序 我比较两个字符串 在java中从字节数组生成的字符串和在asp net第一次解码中接收的字符串相同 但解码后 c
  • 从套接字到套接字的零分配复制

    假设我们有一个套接字连接 我们称其为c1 我们接受消息的地方 我们还有 N 个其他套接字连接 我们将写入完全相同的消息 为了确定我们希望它写入哪个连接 我们只需要读取其中的前几个字节c1 但是套接字上剩余的字节不需要加载到java堆中 只需
  • Javascript 解密 aes-gcm 不起作用,但在 Python 中它可以工作

    我想将一个小型 python 库移植到 JavaScript 在 Node JS 中运行 我收到一条加密消息 初始化向量和密钥 在 python3 中 这些是导入 from cryptography hazmat primitives ci
  • java中的多播

    我正在尝试编写一个简单的多播试验 我使用了标准代码 发送者和接收者 我尝试了一些不同的标准代码 看来接收代码停留在接收状态 就好像没有接收到任何东西一样 接收端 byte b new byte 3 DatagramPacket dgram
  • WCF DataContract 与 DataContract 接口

    WCF 新手 DataContact类可以继承Interface吗 eg DataContract Namespace public class VesselSequence IVesselSequence DataMember publi
  • 使用 Gattle 向 ActiveMQ 发布消息

    我一直在使用Gatling将消息发布到 ActiveMq 服务器 我明白了 java lang SecurityException 无效的用户名 null 或空 不过我使用有效的用户名和密码 这是我的测试代码 抛出了异常 任何有关如何解决此
  • 如何使用Imagick合并和遮罩图像?

    我对图像处理知之甚少 对所使用的术语更是知之甚少 所以请耐心等待 Basically I want to merge two images together where one of them will act as a mask That
  • 如何重置 VisualTreeHelper.GetDpi() 以返回真实的 DPI?

    在 WPF 应用程序中VisualTreeHelper GetDpi 启动应用程序时获取 dpi 即使用户同时在 设置 gt 显示 中更改了 dpi 后续调用也会返回相同的值 我怎样才能 重置 dpiVisualTreeHelper Get
  • 模板意大利面条

    请解释一下那块令人困惑的模板意大利面条 template
  • 如何在 x86_64 主机上使用 Setup.py 构建 32 位 Python 模块分发版

    我需要编译一个 32 位发行版PyEphem http rhodesmill org pyephem 这似乎并不困难 但是 我遇到了一些编译器问题 CFLAGS m32 python setup py bdist p i386 runnin
  • 使用 Javascript/jQuery 确定标题从何处中断到下一行?

    HTML 假设我有固定宽度的容器 其中一些标题将超过一行 我想隔离这些线并对每条线做单独的事情 有没有一种方法 使用 JavaScript 来计算标题中断到下一行的位置 例如在每行周围放置一个跨度 Hacky 和 dirty 在同一个容器中
  • 这种错误查找技术的名称是什么?是否存在可以自动执行该技术的程序?

    假设我有一些巨大的程序P我知道其中有一个错误 当我带参数运行它时A它打印出丑陋的错误消息E 在 shell 表示法中 会发生这种情况 P A E 现在我不知道是什么原因导致了这个错误 但它似乎是确定性的 我找到错误的方法是 隔离 它 我 减
  • Mac 应用程序上的“锁定”屏幕

    在 Mac OS X 中 我需要调用什么 API 才能将窗口不仅覆盖整个屏幕 而且还覆盖菜单栏和停靠栏 另外 是否可以有效地将屏幕 锁定 到这个位置 禁用任务控制 启动板等 我已在应用程序委托的实现文件中尝试了以下代码 void awake
  • Javascript MVC 框架的目的[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我想知道 Javascript MVC 框架 例如 Backbone js 和 Spine js 的用途 作为一名热心 经验丰富的 Ruby o
  • jquery:在选择更改时按名称调用函数

    我正在尝试按名称调用函数 onchange选择但没有任何反应 当功能描述在属性之后时 它就起作用了 这不起作用 HTML
  • 行中的三个按钮 -> 调整大小以适合父级宽度

    我有这个布局 Linear Layout Scroll View Relative Layout 9x 按钮 在这样的视图中 3x3 网格 o o o o o o o o o 每个按钮都有它的背景 没有文字 背景如下