简体   繁体   中英

Android GridView layout and optimization

I have two questions, the first one is that I am creating a bunch of gridviews and the gridview items is just one imageview, when I put a 500 * 1000 image in the items(I only have four items) it lags like crazy but if I insert a lower res pic(100*200) it works fine. So can android not handle this?(I used view recycling too)

The second one is that I want my gridview items to be 125dp wide and have exactly 2dp vertical and horizontal spacing, if there is extra space I would want it to stretch the gridview items, I'm using this code in my xml for my grid view item:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:id="@+id/imageView_icon"
    android:layout_height="wrap_content" >

</ImageView>

and my gridview:

<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:verticalSpacing="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:stretchMode="columnWidth"
        android:scrollbars="none"
        android:columnWidth="125dp"     
        android:horizontalSpacing="2dp"
        android:numColumns="auto_fit" >
    </GridView>

this is what i want:

想

and this is what im getting: 网格视图

try like this,

<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:verticalSpacing="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:stretchMode="columnWidth"
        android:scrollbars="none"
        android:horizontalSpacing="2dp"
        android:numColumns="2" >
    </GridView>

Just add attribute numColumns on your gridview in xml.

android:numColumns="2"

For more detail you have to read below link :-

http://developer.android.com/guide/topics/ui/layout/gridview.html

Gridview's layout code is ok, change grid_item layout like this

<?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" >

    <ImageView
        android:id="@+id/imageView_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/hello_world"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:cropToPadding="false" />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM