7

次のレイアウトファイルがあります。このファイルGridViewImageView背景にはとがあります。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF">
    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginRight="-70dp"
            android:layout_marginBottom="-50dp"
            android:src="@drawable/s_background"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
        <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/gridview"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:columnWidth="90dp"
                  android:numColumns="auto_fit"
                  android:verticalSpacing="10dp"
                  android:horizontalSpacing="10dp"
                  android:stretchMode="columnWidth"
                  android:gravity="center"/>
    </LinearLayout>
</FrameLayout>

これは、グリッドの各「セル」の実際のアイテムに使用するレイアウトです。

<?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">
    <TextView
            android:id="@+id/cardInGrid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:singleLine="true"
            android:textSize="40sp"
            android:textColor="#660099"
            android:typeface="serif"/>
</LinearLayout>

現在、デバイスに次のように表示されています。

ここに画像の説明を入力してください

GridViewの各アイテムを大きくして、画面のサイズに合わせ、ディスプレイの下部に未使用の空白がないようにする方法はありますか?

これはエミュレーターでは正常に機能しますが、デバイスでは画面の解像度が高いため、下部に空白が表示されます。

どうもありがとう

4

3 に答える 3

16

これはうまくいくでしょう。垂直方向の間隔を忘れないでください。

public class MyAdapter extends BaseAdapter {
    public static int ROW_NUMBER = 5;

    public MyAdapter (Context mContext, ArrayList<String> list) {
        this.context = mContext;
        lstDate = list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
        }
        // we need to decrease cell height to take into account verticalSpacing for GridView
        int cellHeight = StrictMath.max(parent.getHeight() / ROW_NUMBER - parent.getContext().getResources().getDimensionPixelOffset(R.dimen.grid_spacing), 320);
        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                parent.getHeight() / ROW_NUMBER);
        convertView.setLayoutParams(param);

        return convertView;
    }
于 2012-06-19T12:55:58.227 に答える
3

自動的ではありません。

特に、セルはテキストです。Androidは、特にワードラップを考慮に入れると、目的を達成するためにテキストの大きさを正確に推測できるわけではありません。

GridView画面いっぱいにデータが足りない場合は、「ディスプレイ下部に未使用の空白」を配置して、複数の画面サイズに柔軟に対応し、データが多い場合はスクロールにも対応できるようにすることがポイントです。。ダッシュボードパターンに似たものを目指している場合は、DashboardLayoutそれらの線に沿ってまたは何かを使用することを検討してください。

于 2011-04-16T23:09:46.080 に答える
0

試しましたandroid:layout_weight="1"か?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:columnWidth="90dp"
              android:numColumns="auto_fit"
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>

または

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
android:layout_weight="1">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/gridview"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:columnWidth="90dp"
              android:numColumns="auto_fit"
              android:verticalSpacing="10dp"
              android:horizontalSpacing="10dp"
              android:stretchMode="columnWidth"
              android:gravity="center"
/>
</LinearLayout>

それがお役に立てば幸いですか?

于 2011-04-16T23:13:08.500 に答える