如何在android中制作全屏表格

发布于 2024-10-06 08:24:11 字数 227 浏览 0 评论 0原文

我正在开发一个 Android 程序,我想制作一个 3x7(3 列,7 行)表格,其中每个单元格都有一个按钮。

我尝试过使用 tableLayout,但每次表格的宽度总是太短或太长,这意味着要么有空白,要么按钮脱离页面。我对高度没有问题,只是宽度有问题。

这个问题也可以通过像 java 中的 GridLayout 这样的布局轻松解决。 Android 中有类似的吗?

任何想法都将不胜感激。

I am working on a program for Android, and I want to make a 3x7 (3 columns, 7 rows) table, where each cell holds a button.

I have tried using tableLayout, but every time the width of the tables are always too short or too long, meaning that there is either blank space or the buttons go off of the page. I am not having a problem with the height, just the width.

This problem could also be solved easily with a layout like GridLayout from java. Is there an equivalent to that in Android?

any ideas are greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-10-13 08:24:11

您必须使用带有权重的线性布局。

尝试一下,我认为就可以了,虽然可能需要一些工作,但它应该让你指明正确的方向。

<?xml version="1.0" encoding="utf-8"?>
<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:"fill_parent" android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout>    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
    </LinearLayout>
</LinearLayout>
</LinearLayout>

You have to use linear layouts with weights.

Try this out, I think that does it, though it might take a little work, but it should get you pointed in the right direction.

<?xml version="1.0" encoding="utf-8"?>
<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:"fill_parent" android:orientation="vertical">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout>    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
            <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
                    android:text="ButtonText"/>
        </LinearLayout> 
    </LinearLayout>
</LinearLayout>
</LinearLayout>
烙印 2024-10-13 08:24:11

也许在按钮上设置 android:layout_width 固定大小“185dp”

maybe set android:layout_width on you button in fix size "185dp"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文