android - 在 Android 中布局 4 个按钮

标签 android layout android-2.2-froyo

我是 Android 开发的新手。我目前正在开发一个应用程序,我需要在屏幕上布置 4 个按钮。按钮 1 + 2 应位于第一行,而 3 + 4 应位于第二行。我希望每个按钮的高度和宽度都相同。由于有多种屏幕尺寸,我希望按钮宽度为屏幕宽度的 40%。 是否可以仅通过布局来制作这样的东西,还是我需要在代码中计算所有内容?

注意:我想将应用程序部署到运行 Android 2.2 或更高版本的设备。

这是一个示例图形。 enter image description here

编辑:对于所有正在寻找方形东西的人。这是解决方案:http://android-layouts.com/category/tags/square

最佳答案

试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:text="Button" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

    <Button
        android:id="@+id/button3"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:text="Button" />

    </LinearLayout>



</LinearLayout>

enter image description here

所以为了解决这个问题你必须使用android:layout_weight="40/100"="0.4"
在它之前,你必须将 android:layout_width 设置为 0。因为没有它 android:layout_weight="40/100"="0.4" 不工作。
有关 ``android:layout_weight` 的更多详细信息,请访问
What does android:layout_weight mean?

更新 1

为按钮的高度做这个任务尝试下面的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="0.6"
        android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="0.4"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="0.6"
        android:text="Button" />

    </LinearLayout>


    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="0.4"
        android:orientation="horizontal" >

    <Button
        android:id="@+id/button3"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="0.4"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0px"
        android:layout_height="fill_parent"
        android:layout_weight="0.6"
        android:text="Button" />

    </LinearLayout>



</LinearLayout>

关于android - 在 Android 中布局 4 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884022/

相关文章:

android - 蓝牙打印机跳过打印行

android - 当我添加新表时没有这样的表

android - 从 Android 生成的构建文件构建 Ant 失败 - 如何修复?

html - 单击按钮时显示 Bootstrap 警报

python - 如何完全删除 tkinter 中标签的垂直填充?

android - 使用 XML 指定 ListView(或其他类似组件)的元素

android - 显示数据库中的值

Java Swing递归布局

android - 为什么 Android 1.5 应用程序不能在 Android 2.2 手机或模拟器上运行?

java - 如何使用微调器更改背景图像