に 2 つの列がありますGridLayout。私がやりたいのは、これらの列がそれぞれ画面の幅の半分を占めるようにし、その子コンテンツが独自のセルの幅/高さを埋めるようにすることです。子をに設定しようとしましfill_parentたが、最初の子がレイアウト全体を引き継ぐだけです。GridLayout はサポートしていないようですweight。使用するより良いレイアウトがあるかもしれませんが、グリッド スタイルのレイアウトが必要なので、それが自然な選択のように思えます。
6 に答える
このコードは、API21 より前のサポート ライブラリで利用できます。
使用可能なスペースの 50% を占める 2 列の gridLayout に 4 つのボタンを表示する簡単なコードがあります。
<GridLayout
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    >
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />
       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />
       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />
       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />
</GridLayout>
解決策はおそらくこれです:
android:layout_gravity="fill"
android:layout_columnWeight="1"
わかりましたので、グリッド ビューをあきらめて、いくつかの線形レイアウトを使用しました。私は縦のものを作り、次に横のものを2つ追加しました。グリッドビューよりも少し複雑ですが、少なくともこれが機能することを理解するまでは。
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
        <ImageButton
            android:id="@+id/btn_mybutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/pomegranate"
            android:contentDescription="@string/contentDescriptionmybutton"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
        <ImageButton
            android:id="@+id/btn_prefs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/pomegranate"
            android:contentDescription="@string/contentDescriptionSettings"
            android:src="@drawable/ic_settings" />
    </LinearLayout>
</LinearLayout>
そして、これを追加してボタンを正方形にします:)
@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  btnPrefs.setMinimumHeight(btnPrefs.getWidth());
  btnVerse.setMinimumHeight(btnMyButton.getWidth());
 }
RelativeLayout クラスを拡張して (または必要に応じて LinearLayout を使用して)、アイテムの高さが高さと同じになるようにすることができます。
public class GridItem extends RelativeLayout {
        public GridItem(Context context) {
                super(context);
        }
        public GridItem(Context context, AttributeSet attr) {
                super(context, attr);
        }
        public GridItem(Context context, AttributeSet attr, int integer) {
                super(context, attr, integer);
        }
        // Override onMeasure to give the view the same height as the specified width
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, widthMeasureSpec);
            setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
        }
}
アイテム レイアウトの親ビューは、確実に機能するように GridItem ビューにする必要があります。これは、ListAdapter の getView でインフレートするレイアウト ファイルである必要があります。
<?xml version="1.0" encoding="utf-8"?>
<my.packagename.GridItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The content of the item -->
</my.packagename.GridItem>
そして、GridView の stretchMode を columnWidth に設定します。これにより、すべてのアイテムが指定された幅の列数に収まるようになります。新しいビューでは、高さも同じになります。
<GridView
    android:id="@+id/gridList"
    android:numColumns="2"
    android:stretchMode="columnWidth"
/>
を使用すると、GridLayoutManagerを使用できますsetSpanSizeLookup。これは、このメソッドを適切に使用するのに役立つはずの私のプロジェクトのスニペットです。
if (mAdapter == null) {
    final int columnCount = getResources().getInteger(R.integer.numberGridColumns);
    mLayoutManager = new GridLayoutManager(getActivity(), columnCount);
    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            switch (mAdapter.getItemViewType(position)) {
                case ListAdapter.VIEW_TYPE_ONE_COLUMN:
                    return columnCount;
                case RecipeListAdapter.VIEW_TYPE_FULL_COLUMN:
                default:
                    return 1;
            }
        }
    });
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new RecipeListAdapter(mPresenter);
    mRecyclerView.setAdapter(mAdapter);
}
mAdapter.notifyDataSetChanged();