私はGridLayout
に2列あります。私がしたいのは、これらの列をそれぞれ画面の幅の半分にし、その子の内容をセルの幅/高さに合わせることです。私は子供をfill_parent
に設定しようとしましたが、それは最初のレイアウトをレイアウト全体に引き継ぐだけです。 GridLayoutはweight
をサポートしていないようですか?おそらく、より良いレイアウトが使用されるかもしれませんが、私はグリッドスタイルのレイアウトが自然な選択のように思えます。GridLayout列幅
答えて
私はグリッドビューをあきらめて、いくつかの線形レイアウトを使用しました。私は垂直のものを作ってから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());
}
私は自分のボタンを長くしたいと思っていました。あなたが提供したコードを使用しました。それは動作しますが、なぜ 'Activity'の' onCreate'に効果がないのだろうと思います。何か案は? – MikkoP
あなたは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"
/>
このコードは、サポートライブラリの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"
サポートライブラリの最新リリースで、1つ前のAPI 21を入手できるようになりました。依存関係を追加するだけです - com.android.support:gridlayout-v7:22.0.0 – oznus
@oznus私はこれを試しましたが、まだ使用していません。その重量のプロパティを取っていない。 –
新しい 'GridLayout'を使うときは、' app'ネームスペースを使うべきですので、 'app:layout_gravity =" fill "' –
、サポートライブラリを使用します。
を追加210compile '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ファイルに
を
xmlns:app="http://schemas.android.com/apk/res-auto"
を追加することを忘れていけません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();
は、利用可能なスペースの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>
- 1. Vaadin GridLayoutのカスタム幅
- 2. GridLayout - JButton、配列、イメージ?
- 3. Gridlayoutセル幅のサイズが間違っています
- 4. GridLayoutの色を異なる幅に設定する方法
- 5. 等幅の行を含むGridLayoutを作成する
- 6. GridLayoutには、私が使用して作業するのGridLayoutを取得しようとしてきた幅
- 7. ランダム列番号は、一つのGridLayout
- 8. のGridLayout
- 9. アンドロイド - GridLayoutの
- 10. AndroidスタジオGridLayout
- 11. Androidスタジオ:Flexible Gridlayout
- 12. GridLayoutには
- 13. KotlinのGridLayout?
- 14. Xamarin Android Fluid GridLayout
- 15. kivy GridLayoutとAnchorLayout
- 16. GridLayout(1,1)でスクロールバー
- 17. GridLayoutのスクロール
- 18. Java Repaint gridlayout
- 19. Android GridLayout Dividers
- 20. Android GridLayout Fitting Error
- 21. GridLayoutとダイナミックビュー
- 22. ScrollledComposite親とGridLayout
- 23. GridLayout内の特定のセルの幅と高さを見つける方法
- 24. GridLayoutのネストされた要素の重みと幅を指定する方法
- 25. SWT - GridLayoutで第1列と第2列を使用しない
- 26. qdialogとscrollareaとgridlayout
- 27. 2x2 imageviewのGridlayout android
- 28. eclipseのGridLayoutとGridData
- 29. アンドロイドのGridLayout columnSpanは
- 30. Java Swing GridLayout "Cell span"?
特定の高さ/幅を入力しようとしていますか?そして、すべてのグリッドアイテムを同じ高さにしたいですか? –
はい。私は2列あります。私はその幅を画面幅の50%にしたいので、一緒に画面の幅に等しくなります。その高さはピクセル単位の幅と同じになります。だから彼らは正方形になるでしょう。 – bwoogie
解決策は見つかりましたか?はいの場合は共有してください。私も同じ種類の機能が必要です。 – BSKANIA