如何在Android xml布局的3行中放置9个按钮? [英] how to put 9 buttons in 3 rows in android xml layout?

查看:85
本文介绍了如何在Android xml布局的3行中放置9个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作井字游戏(Android版). 我要针对设备的宽度和高度使所有9个按钮自动调整大小,然后将它们均匀地放在3 * 3的网格中.但是我现在只能设置其大小的数字. 谁能告诉我如何让他们使用父母的身高和宽度并计算他们的尺寸?

Im trying to make a tic-tac-toe game(android version). I want to make all the 9 buttons auto-sizing regarding to the width and the height of the device and put them evenly in a 3*3 grid. But I can only set the number for their sizes now. Can anyone tell me how to let them use the height and width of the parent and calculate their sizes?

此外,我现在正在使用网格布局.这是我在井字游戏中应该使用的最佳布局吗?

Also, im using the grid layout now. Is this the best layout I should use for the tic-tac-toe game?

谢谢.

推荐答案

LinearLayoutsandroid:weightSumandroid:layout_weight

修改

示例:

<?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"
    android:weightSum="3" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="3" >

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

这篇关于如何在Android xml布局的3行中放置9个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆