[Android]GridViewの表示について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
ひよつこ

[Android]GridViewの表示について

#1

投稿記事 by ひよつこ » 11年前

こんにちは、ひよつこです。
今回もよろしくお願いします。

現在、端末内にインストールされているアプリケーションのアイコンと名前を
一覧にしてGridViewに表示する処理をさせようと思っています。
そこで以下のようなコードを書きました。

MainActivity.java

コード:

public class MainActivity extends Activity {

	private List<AppData> appList = new ArrayList<AppData>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_grid);
		CustomGridAdapter customAdapter = new CustomGridAdapter(this,0,appList);
		final GridView mGridView = (GridView) findViewById(R.id.app_grid);
		mGridView.setAdapter(customAdapter);
		PackageManager pManager = this.getPackageManager();
		Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
		final List<ResolveInfo> rInfoList = pManager.queryIntentActivities(intent,0);
		for(ResolveInfo rInfo : rInfoList){
			AppData appInfo = new AppData();
			if(rInfo != null){
				appInfo.appName = rInfo.loadLabel(pManager).toString();
				try{
					Drawable icon = pManager.getApplicationIcon(appInfo.appPackage);
					appInfo.appIcon = icon;
					appList.add(appInfo);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		Collections.sort(appList, appComparator);
	}

	private static final Comparator<AppData> appComparator = new Comparator<AppData>(){
		private final Collator collator = Collator.getInstance();
		@Override
		public int compare(AppData map1, AppData map2) {
			return collator.compare(map1.appName, map2.appName);
		}
	};
}
AppData.java

コード:

public class AppData{
	Drawable	appIcon;
	String		appName;
}
CustomGridAdapter.java

コード:

public class CustomGridAdapter extends ArrayAdapter<AppData>{

	private LayoutInflater lInflater;

	public CustomGridAdapter(Context context,int ResourceId,List<AppData> objects) {
		super(context, ResourceId, objects);
		lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position,View convertView,ViewGroup parent) {
		AppData item = getItem(position);
		ViewHolder holder;
		if(convertView == null){
			convertView = lInflater.inflate(R.layout.grid_layout, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.grid_app_name);
			holder.image = (ImageView) convertView.findViewById(R.id.grid_app_icon);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(item.appName);
		holder.image.setImageDrawable(item.appIcon);
		return convertView;
	}
}
main_grid.xml

コード:

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

	<GridView
		android:id="@+id/app_grid"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:stretchMode="columnWidth"
		android:numColumns="auto_fit"
		android:background="#00000000" />

</LinearLayout>
grid_layout.xml

コード:

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

	<ImageView
		android:id="@+id/grid_app_icon"
		android:layout_width="60dp"
		android:layout_height="60dp"
		android:contentDescription="@string/app_name"
		android:background="#c8ffffff" />

	<TextView
		android:id="@+id/grid_app_name"
		android:textColor="#ffffff"
		android:layout_width="70dp"
		android:layout_height="wrap_content"
		android:background="#64000000"
		android:maxLines="1" />

</LinearLayout>
しかし、上記のコードを実機で動かしてみたところ、
GridViewは横に2列しか項目が表示されず、だいぶ隙間が空いていました。
しかも、項目が表示されるのはエリアの左側に偏っており真ん中に表示されません。

GridViewの項目をできる限り画面に敷き詰める方法と
項目をエリアの中心に表示させる方法を教えていただきたいと思います。
毎度説明が下手で恐縮ですが、どうかよろしくお願いします。

ISLe
記事: 2650
登録日時: 14年前
連絡を取る:

Re: [Android]GridViewの表示について

#2

投稿記事 by ISLe » 11年前

grid_layout.xmlで項目の幅が合計130dpなので2列になるのは自然なことではないでしょうか。

水平方向にセンタリングするのは、grid_layout.xmlのLinearLayoutに
android:layout_gravity="center_horizontal"
を追加すれば良いはずです。

ひよつこ

Re: [Android]GridViewの表示について

#3

投稿記事 by ひよつこ » 11年前

返信ありがとうございます。
ひよつこです。

grid_layout.xmlのLinearLayoutにandroid:layout_gravity="center_horizontal"を
追加してもレイアウトに変化はありませんでした。
しかし、LinearLayout内のImageViewとTextViewに"center_horizontal"を追加した
ところ、上手くいきました。

それとは別にGridViewの横列の数についてですが、
android:numColumns="4"にしたところ実機では正しく表示されていました。
しかし"auto_fit"にするとやはり2列しか表示されません。

何が問題なのでしょうか。

ISLe
記事: 2650
登録日時: 14年前
連絡を取る:

Re: [Android]GridViewの表示について

#4

投稿記事 by ISLe » 11年前

Android端末の横幅(狭い方)は、320dp前後です。
ImageViewの横幅が60dp、TextViewの横幅が70dpで、合計130dpですから、320dpに対して2列になるのが自然なのではないでしょうか。

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: [Android]GridViewの表示について

#5

投稿記事 by へにっくす » 11年前

狭い方の幅はどんな解像度でも320dpですよ。
※フォルダres/layoutにおいてある場合、通常normalと判断されます。
Supporting Multiple Screens - Android Developers
WebデザイナーのためのAndroid用レイアウト講座-Chapter10: 単位について(dp, sp, px)
written by へにっくす

ISLe
記事: 2650
登録日時: 14年前
連絡を取る:

Re: [Android]GridViewの表示について

#6

投稿記事 by ISLe » 11年前

へにっくす さんが書きました:狭い方の幅はどんな解像度でも320dpですよ。
世の中には変態的なサイズの実機端末もあるかと思ったんですがそうではないんですね。
Android Developersに"at least"の記述があるのが気になりますけど。

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: [Android]GridViewの表示について

#7

投稿記事 by へにっくす » 11年前

ISLe さんが書きました: 世の中には変態的なサイズの実機端末もあるかと思ったんですがそうではないんですね。
Android Developersに"at least"の記述があるのが気になりますけど。
at leastには①少なくとも、最少に見ても②たった~しか、~であること(だけ)は確か、という意味がありますが
どちらかというと、これは②で最低限dpという単位で、この仕様だけは満たせよという解釈になるのかな~と。
「以上」でも「以下」でもないと思います。
at leastの意味・用例 - 英辞郎 on the web
written by へにっくす

ISLe
記事: 2650
登録日時: 14年前
連絡を取る:

Re: [Android]GridViewの表示について

#8

投稿記事 by ISLe » 11年前

へにっくすさんがリンクしたブログ記事にも書いてありますけど、現実にそれより大きいサイズの端末が存在します。

このサイズ以上でなければならない、という意味だと思いますけど。

アバター
へにっくす
記事: 634
登録日時: 13年前
住所: 東京都

Re: [Android]GridViewの表示について

#9

投稿記事 by へにっくす » 11年前

ISLe さんが書きました:へにっくすさんがリンクしたブログ記事にも書いてありますけど、現実にそれより大きいサイズの端末が存在します。
このサイズ以上でなければならない、という意味だと思いますけど。
ああそうか、狭い方ばっかり注目してました。
Android Developersの説明では、
normal screens are at least 470dp x 320dp
私が示したブログの該当箇所とはここですね?
800x480(hdpi)の端末だと、533dp x 320dpでした。
480x320(mdpi)の端末だと、480dp x 320dpでした。
これで~以上と見なせますね。指摘有難うございます。

割り込み失礼しました。。
written by へにっくす

ひよつこ

Re: [Android]GridViewの表示について

#10

投稿記事 by ひよつこ » 11年前

返信ありがとうございます。

どうやらdpの認識が間違っていたようです。
丁寧な説明ありがとうございました。

これからもよろしくお願いします。

閉鎖

“C言語何でも質問掲示板” へ戻る