Подтвердить что ты не робот

Настройте GridView на все размеры экрана

Я пытаюсь реализовать GridView как часть галереи изображений. Я выполнил следующий пример из Android-разработчика портала.

Учебник, похоже, работает для всех размеров экрана. Как вы можете видеть ниже, пропорции для небольшого и большого размера экрана отображаются правильно (слева - маленький размер экрана, справа - большой размер экрана).

GridView tutorial from android developer portal

Но теперь к моей проблеме. Когда я хочу реализовать точно такой же GridView в LinearLayout из моего Android-проекта - правильные пропорции ушли - как показано на рисунках ниже. Изображения начинают перекрываться так далее и т.д.

My android project

Я совершенно уверен, что это имеет кое-что для моего LinearLayout, который выглядит следующим образом.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/widget64"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical">

    <LinearLayout
            android:id="@+id/widget34"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:background="@drawable/foto_detail_bg_cell_0"
            >
        <ImageView
                android:id="@+id/flyer"
                android:layout_width="100dp"
                android:layout_height="80dp"
                android:src="@drawable/icon"
                android:scaleType="centerCrop"
                />
        <LinearLayout
                android:id="@+id/widget36"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5px"
                android:orientation="vertical">
            <TextView
                    android:id="@+id/toptext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Beat.It"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    android:textColor="@color/white"/>
            <TextView
                    android:id="@+id/widget39"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="http://www.google.at"
                    android:textColor="@color/white"/>
            <LinearLayout
                    android:id="@+id/widget40"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                <ImageView
                        android:id="@+id/widget41"
                        android:layout_width="58dp"
                        android:layout_height="40dp"
                        android:src="@drawable/facebook_share"/>
                <ImageView
                        android:id="@+id/widget42"
                        android:layout_width="58dp"
                        android:layout_height="40dp"
                        android:layout_marginLeft="1dp"
                        android:src="@drawable/photocount"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <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:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
            />


</LinearLayout>

ГЛАВНЫЙ ВОПРОС: В XML-макете, используемом в учебном пособии портал разработчика Android, используется только GridView без LinearLayout, и он отображается с правильными пропорциями на каждом Размер экрана. Почему это? И почему это не работает должным образом, когда я вставляю GridView в Linear Layout, как в моем проекте?

4b9b3361

Ответ 1

РЕШИТЬ:

Я решил эту проблему, отредактировав ImageAdapter GridView. Я вычислил размер сгенерированных представлений изображений - независимо от плотности.

// 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) {  

            //Calculation of ImageView Size - density independent.
            //maybe you should do this calculation not exactly in this method but put is somewhere else.
            Resources r = Resources.getSystem();
            float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());


            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //imageView.setPadding(8, 8, 8, 8);
            imageView.setBackgroundColor(Color.BLUE);
        } else {
            imageView = (ImageView) convertView;
        }

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