【问题标题】:How to make gridview elements fit to the screen without scrollbar?如何在没有滚动条的情况下使 gridview 元素适合屏幕?
【发布时间】:2012-08-28 22:59:49
【问题描述】:

我在一个屏幕上有两个部分(两个片段:第一个片段覆盖 60% 的屏幕,第二个占据剩余部分。)。在第一个片段中我放置了gridview,在第二个片段中我创建了tableLayout。在 gridView 中,我放置了 36 个 ediText。在下 tablelayout 中,我设计了自定义键盘。由于我需要将十个编辑文本显示为完全适合第一个片段,因此我禁用了 gridview 的垂直滚动条。即使在纵向模式下它在模拟器上有点工作,它在横向模式下也不起作用。所以,我决定先获取fragment的高度,那么有没有办法获取fragment的高度和宽度。即使在获得措施之后,当模式发生变化时,如何使我的编辑文本适合当前片段?虽然我搜索了资源,但找不到明确的解决方案。以下是我的代码。任何建议将不胜感激。提前致谢。

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

        <FrameLayout
                android:id="@+id/fLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:background="@drawable/background3">
        <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/gridview"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:columnWidth="18dp"
                android:numColumns="9"
                android:verticalSpacing="0dp"
                android:horizontalSpacing="5dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:listSelector="@null"/>
        </FrameLayout>

        <FrameLayout
                android:id="@+id/fLayout2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="4"
                android:background="@drawable/background2">

        <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/keypad"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:stretchColumns="*">

        <TableRow>
        <Button android:id="@+id/keypad_1"
        android:text="1"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_2"
        android:text="2"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_3"
        android:text="3"
        android:background="@drawable/custombutton">
        </Button>

        <Button android:id="@+id/keypad_4"   
        android:text="4"
        android:background="@drawable/custombutton">
        </Button>

        <Button android:id="@+id/keypad_5"
        android:text="5"
        android:background="@drawable/custombutton">
        </Button>

        </TableRow>
        <TableRow>



        <Button android:id="@+id/keypad_6"
        android:text="6"
        android:background="@drawable/custombutton">
        </Button>

        <Button android:id="@+id/keypad_7"
        android:text="7"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_8"
        android:text="8"
        android:background="@drawable/custombutton">
        </Button>
        <Button android:id="@+id/keypad_9"
        android:text="9"
        android:background="@drawable/custombutton"> 
        </Button>

        <Button android:id="@+id/keypad_10"
        android:text="C"
        android:background="@drawable/custombutton">
        </Button>

        </TableRow>
        <TableRow>
        <Button android:id="@+id/submit"
        android:text="validate"
        android:layout_span="5"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/custombutton">

        </Button>
        </TableRow>
        </TableLayout>

        </FrameLayout>
</LinearLayout>

【问题讨论】:

    标签: android gridview height android-edittext


    【解决方案1】:

    gridView 总是可以滚动的。您可以创建一个具有固定大小的自定义(在某处有示例)

    如果您知道您将有 10 个元素,请使用静态布局(例如使用表格布局)

    【讨论】:

    • 对不起,我有 36 个编辑文本。所以很简单,用 Adapter 类完成了 36 个编辑文本。如果我使用表格布局或其他布局,它会使代码冗长。
    • 真的。你可以尝试制作一个自定义的不可滚动的gridview。诀窍是修改onMeasure。查看这个问题的答案stackoverflow.com/questions/4523609/…
    • 嗨,你能告诉我哪里出错了。正如您所建议的,我创建了自定义网格视图。当我在我的活动类中添加以下代码时,应用程序显示“应用程序已意外停止”。 gridView = (ExpandableHeightGridView) findViewById(R.id.myId); gridView.setAdapter(new TextAdapter(this));
    • 用代码和完整的堆栈跟踪发布一个新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2013-12-11
    • 2021-04-21
    • 2017-07-02
    • 2021-07-17
    相关资源
    最近更新 更多