【问题标题】:Grid view in xml androidxml android中的网格视图
【发布时间】:2014-05-03 17:39:21
【问题描述】:

我想在GridView 中显示ImageButtons。目前我正在使用RelativeLayout

这是我的 XML 代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <RelativeLayout
    android:id="@+id/RelativeLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.easy.convert.MainActivity" >

    <ImageButton
        android:id="@+id/btnbitsbytes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:background="#00000000"
        android:gravity="center"
        android:src="@drawable/btnbitsbytes" />

    <ImageButton
        android:id="@+id/btnmassweight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnbitsbytes"
        android:layout_below="@+id/btnbitsbytes"
        android:layout_marginTop="14dp"
        android:background="#00000000"
        android:src="@drawable/btnmassweight" />

    <ImageButton
        android:id="@+id/ButtonConvert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btnmassweight"
        android:layout_below="@+id/btnmassweight"
        android:layout_marginTop="14dp"
        android:background="#00000000"
        android:src="@drawable/btnlength" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ButtonConvert"
        android:layout_below="@+id/ButtonConvert"
        android:layout_marginTop="14dp"
        android:background="#00000000"
        android:src="@drawable/btntemperature" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton3"
        android:layout_below="@+id/imageButton3"
        android:layout_marginTop="15dp"
        android:background="#00000000"
        android:src="@drawable/btndistance" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:background="#00000000"
        android:src="@drawable/btnaboutus" />

    </RelativeLayout>
</ScrollView>

我不知道如何使用 GridView 或任何其他布局,但我希望我的布局看起来像这样:

【问题讨论】:

    标签: android xml gridview layout imagebutton


    【解决方案1】:

    您不需要ScrollViewGridView。 gridview 小部件有自己的滚动检测,它可以干扰滚动视图滚动。我建议你做一些事情来实现你想要的,试着在你的滚动视图里面放一个TableLayout,这样会更适应:

    <ScrollView ... >
        <RelativeLayout ... >
            <TableLayout ...>
                <TableRow>
                     <ImageButton>
                     <ImageButton>
                     <ImageButton>
                </TableRow>
                <TableRow>
                     <ImageButton>
                     <ImageButton>
                     <ImageButton>
                </TableRow>
                <TableRow>
                     <ImageButton>
                     <ImageButton>
                     <ImageButton>
                </TableRow>
        </RelativeLayout>
    </ScrollView>
    

    或者你可以只添加 3 个LinearLayouts 并将每个孩子的weight 属性设置为1,你的布局将是这样的:

    <ScrollView ... >
    
        <RelativeLayout ... >
    
            <LinearLayout ...
                android:orientation="horizontal"
                android:weightSum="3" >
    
                <ImageButton   ...
                    android:layout_weight="1" />
    
                <ImageButton  ...
                    android:layout_weight="1" />
    
                <ImageButton  ...
                    android:layout_weight="1" />
    
            </LinearLayout>
    
        <RelativeLayout>
    
    </ScrollView>  
    

    weightSum 设置为3 表示线性布局中的列数,layout_weight 表示每个子级将填充1 列。

    但如果你绝对想要GridView,你需要这个布局:

    <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="90dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />
    

    您还需要一个Adapter 来在里面显示您的图片。你应该阅读这个主题:GridView但 GridView 不允许有页脚视图,例如您的“关于我们”按钮..
    要在最后保留按钮 “关于我们”,您需要为您的 gridview 自定义适配器,也许这会对您有所帮助:HFGridView - 它的工作方式类似于 addFooterView for ListView

    【讨论】:

    • 正如我在您的屏幕截图中看到的那样,当您只有 9 张图像时,我会更容易和更适应使用 tablelayout 而不是 gridview.. ;)
    • 不,实际上我正在考虑添加更多图像(选项)。 TableLayout 会在不同的屏幕尺寸下产生问题吗?
    • 不,但是如果你有多个图像(比如 20 个,甚至更多),它会很重,因为它都是手动显示的,而不是像使用适配器显示行布局的 GridView 或 ListView 那样动态显示。我的意思是你必须在你的布局中写下所有 20 个视图。你明白吗?
    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多