2013-09-02 11 views
35

私はGridLayoutに2列あります。私がしたいのは、これらの列をそれぞれ画面の幅の半分にし、その子の内容をセルの幅/高さに合わせることです。私は子供をfill_parentに設定しようとしましたが、それは最初のレイアウトをレイアウト全体に引き継ぐだけです。 GridLayoutはweightをサポートしていないようですか?おそらく、より良いレイアウトが使用されるかもしれませんが、私はグリッドスタイルのレイアウトが自然な選択のように思えます。GridLayout列幅

+1

特定の高さ/幅を入力しようとしていますか?そして、すべてのグリッドアイテムを同じ高さにしたいですか? –

+2

はい。私は2列あります。私はその幅を画面幅の50%にしたいので、一緒に画面の幅に等しくなります。その高さはピクセル単位の幅と同じになります。だから彼らは正方形になるでしょう。 – bwoogie

+0

解決策は見つかりましたか?はいの場合は共有してください。私も同じ種類の機能が必要です。 – BSKANIA

答えて

5

私はグリッドビューをあきらめて、いくつかの線形レイアウトを使用しました。私は垂直のものを作ってから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()); 

} 
+0

私は自分のボタンを長くしたいと思っていました。あなたが提供したコードを使用しました。それは動作しますが、なぜ 'Activity'の' onCreate'に効果がないのだろうと思います。何か案は? – MikkoP

0

あなたは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

<?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> 

のgetViewメソッドで膨らませるとcolumnWidthのへのGridViewのstretchModeを設定しますレイアウトファイルでなければなりません。これにより、すべての項目が指定した幅の列に収まるようになります。新しいビューは、同じ高さを持つことを確認します。

<GridView 
    android:id="@+id/gridList" 
    android:numColumns="2" 
    android:stretchMode="columnWidth" 
/> 
32

このコードは、サポートライブラリのpre 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> 

ソリューションは、おそらくこれで助けることができる を事前API 21

android:layout_gravity="fill" 
android:layout_columnWeight="1" 
+13

サポートライブラリの最新リリースで、1つ前のAPI 21を入手できるようになりました。依存関係を追加するだけです - com.android.support:gridlayout-v7:22.0.0 – oznus

+1

@oznus私はこれを試しましたが、まだ使用していません。その重量のプロパティを取っていない。 –

+0

新しい 'GridLayout'を使うときは、' app'ネームスペースを使うべきですので、 'app:layout_gravity =" fill "' –

7

、サポートライブラリを使用します。

を追加210
compile 'com.android.support:appcompat-v7:23.1.1' 
compile 'com.android.support:design:23.1.1' 

あなたの依存関係。あなたのxmlファイルで次に

:ここ

<android.support.v7.widget.GridLayout 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:columnCount="2" 
        app:orientation="horizontal" 
        app:rowCount="1"> 

        <TextView 
         android:text="1" 
         android:textStyle="bold" 
         app:layout_columnWeight="1" 
         /> 

        <TextView 
         android:text="2" 
         android:textStyle="bold" 
         app:layout_columnWeight="1" /> 

</android.support.v7.widget.GridLayout> 

は "アプリ" プレフィックスの使用を注意して使用すると、あなたのxmlファイルに

0

xmlns:app="http://schemas.android.com/apk/res-auto" 

を追加することを忘れていけませんGridLayoutManagersetSpanSizeLookupを使用することができます。ここでは、適切にこの方法を使用するのに役立つはずです私のプロジェクトからの抜粋です:

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(); 
1

は、利用可能なスペースの50%を取る2列のグリッドレイアウトで動的にビューを追加:

GridLayout gridLayout = new GridLayout(); 

View view; //it can be any view 

GridLayout.LayoutParams param = new GridLayout.LayoutParams(); 

param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f); 

param.width = 0; 

view.setLayoutParams(param); 

gridLayout.add(view); 

を静的な方法(.xmlファイル)。

<android.support.v7.widget.GridLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:alignmentMode="alignBounds" 
    app:columnCount="2" 
    app:columnOrderPreserved="false" 
    app:orientation="horizontal" 
    android:layout_margin="20dp" 
    android:layout_marginBottom="30dp" 
    android:padding="4dp" 
    app:rowCount="2"> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_username" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="0" 
    app:layout_row="0" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    android:text="@string/user_name" /> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_first_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="1" 
    app:layout_row="0" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    android:text="@string/first_name" /> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_middle_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="0" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    app:layout_row="1" 
    android:text="@string/middle_name" /> 

<TextView 
    android:layout_marginTop="2dp" 
    android:id="@+id/edit_profile_last_name" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    app:layout_column="1" 
    app:layout_gravity="fill" 
    app:layout_columnWeight="1" 
    app:layout_row="1" 
    android:text="@string/last_name" /> 

</android.support.v7.widget.GridLayout>