2

いくつかの場所についてのギャラリーを作成しています。私のアプリには、さまざまな場所の約 20 個の画像ボタンを持つ ScrollView が含まれており、ユーザーがいずれかの場所をクリックすると、その場所に関する情報を含む別の画面に移動します。

これまでのところ、アプリを完成させることができました.唯一の問題は、場所の画像ボタンが次々と配置されていることですが、スペースがあれば並べて配置したい. たとえば、現在私のアプリは次のようになっています。

ここに画像の説明を入力

画面の左右に無駄なスペースがたくさんあるので、できればアプリのレイアウトをこのようにしたいです。

ここに画像の説明を入力

ランドスペースモードのときは、このようなものが欲しいです。 ここに画像の説明を入力

また、可能であれば、電話の画面サイズによっては、現在のビューに収まる画像を増やしたいと考えています。たとえば、アプリが 10 インチのタブレットで開かれている場合、同じ行に 2 つのイメージボタンを配置するのではなく、もっと多くのボタンを配置したいとします。

私の XML コードは次のとおりです。

        <ImageButton
            android:id="@+id/london"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/london"
            android:src="@drawable/london" />

        <ImageButton
            android:id="@+id/paris"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/paris"
            android:src="@drawable/paris" />

....


        <ImageButton
            android:id="@+id/berlin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/berlin"
            android:src="@drawable/berlin" />

ご協力いただきありがとうございます。私が自分自身を明確に説明していない場合はお知らせください。よりよく説明できるよう最善を尽くします。

4

1 に答える 1

0

これは のユースケースのように聞こえますGridView。良いニュースは、おそらくアダプターを再利用でき、 を に置き換えるだけでよいことListViewですGridViewここにドキュメントへのリンクがあります

- 編集 -

を置き換えたい場合はScrollView、アプローチを変更する必要があります。

xml ファイルでは、すべての ImageButtons を単一の に置き換えることができますGridView:

<GridView
    android:id="@+id/gvCities"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</GridView>

という名前の新しいレイアウト ファイルが必要ですcity_button

<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:contentDescription="@string/content_discription"
    android:layout_height="wrap_content" >

</ImageButton>

新しいクラスを作成する必要があります。CityAdapter

import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;

import com.znappie.activities.R;

public class CityAdapter extends BaseAdapter {
    private int[] imageIds;
    private LayoutInflater inflate;
    private Resources resources;

    public CityAdapter(int[] imageIds, LayoutInflater inflate,
            Resources resources) {
        this.imageIds = imageIds;
        this.inflate = inflate;
        this.resources = resources;
    }

    @Override
    public int getCount() {
        return imageIds.length;
    }

    @Override
    public Object getItem(int position) {
        return imageIds[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflate.inflate(R.layout.city_button, parent);
        }
        ((ImageButton) convertView).setImageDrawable(resources
                .getDrawable(imageIds[position]));
        return convertView;
    }

}

その後、メイン アクティビティからこれを使用できます。

int[] ids = {R.drawable.berlin, R.drawable.london, ...};
            GridView cities = (GridView) findViewById(R.id.gvCities);
            cities.setAdapter(new CityAdapter(ids, getLayoutInflater(), getResources()));
于 2015-01-11T11:12:02.990 に答える