SORU
2 AĞUSTOS 2011, Salı


Nasıl Denetim auto_fit sütun sayısını bulmak mı?Android:

Gridview nasıl çalıştığını daha iyi anlamak için, özellikle auto_fitistiyorum. Burada XML düzeni:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@ id/gridview"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:columnWidth="60dp"
    android:numColumns="auto_fit"
/>

Ve altı küçük (48*48 piksel) bir dizi ile iyi çalışır. Portre modunda, bir satır, altı sütun görüntüler.

with

Benim anlamadığım android:columnWidth="60dp" auto_fit sütun doğru numarayı bulmak için bekleniyor, çünkü bu gereklidir, nedeni budur.
Satır android:columnWidth="60dp" kılavuz 3 satır ve 2 sütun görüntüler.

without

Burada ImageAdapter sınıf:

package com.examples.HelloGridView;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setPadding(0, 0, 0, 0);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.ic_1, R.drawable.ic_2,
            R.drawable.ic_3, R.drawable.ic_4,
            R.drawable.ic_5, R.drawable.ic_6
    };
}

Yardımlarınız için teşekkür ederim.

CEVAP
24 EKİM 2011, PAZARTESİ


Denetim kaynağı bakıyor, İmageView üzerinde dolgu ve yükseklik ayarı yardımcı olmayacağı açıktır. Sütun genişliği belirtilmezse, sütun (2) önceden belirlenmiş bir sayı seçer:

    private void determineColumns(int availableSpace) {

    ...

    if (mRequestedNumColumns == AUTO_FIT) {
        if (requestedColumnWidth > 0) {
            // Client told us to pick the number of columns
            mNumColumns = (availableSpace   requestedHorizontalSpacing) /
                    (requestedColumnWidth   requestedHorizontalSpacing);
        } else {
            // Just make up a number if we don't have enough info
            mNumColumns = 2;
        }
    } else {
        // We picked the columns
        mNumColumns = mRequestedNumColumns;
    }

    if (mNumColumns <= 0) {
        mNumColumns = 1;
    }

    ...

Çözüm Denetim sütun genişliği ayarı önce sütun boyutunu ölçmek için. Burada Görüş ekran ölçmek için hızlı bir yol

public int measureCellWidth( Context context, View cell )
{

    // We need a fake parent
    FrameLayout buffer = new FrameLayout( context );
    android.widget.AbsListView.LayoutParams layoutParams = new  android.widget.AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    buffer.addView( cell, layoutParams);

    cell.forceLayout();
    cell.measure(1000, 1000);

    int width = cell.getMeasuredWidth();

    buffer.removeAllViews();

    return width;
}

Ve ardından hemen Denetim sütun genişliği ayarlayın:

gridView.setColumnWidth( width );

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ELawshea

    ELawshea

    26 Mayıs 2008
  • FattySpins's channel

    FattySpins's

    17 Mayıs 2009
  • Leigh Momii

    Leigh Momii

    10 Mayıs 2006