我有2个列中有6个图像的GridView.我想在屏幕中间显示这6张图像.我将重力属性设置为居中,但此中心元素仅水平放置.GridView占据整个屏幕.
<GridView
android:id="@+id/gridview"
android:layout_above="@id/ad" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#ffffff"
android:gravity="center"
/>
Run Code Online (Sandbox Code Playgroud)
Ray*_*ter 54
以下是我将gridview中的项目置于中心位置(注意拉伸模式,列宽,重力以及水平和垂直间距):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="10dp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我花了一段时间来弄清楚它,但是弄乱了那些不同的价值观,我才能让它们成为中心.
Nel*_*oso 13
尝试将GridView包装在LinearLayout中,使其子元素居中,并将gridview的layout_height更改为"wrap_content".例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_gravity="center">
<GridView
android:id="@+id/homeGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
小智 8
需要设置以下属性:
android:stretchMode="columnWidth"
Run Code Online (Sandbox Code Playgroud)
...和...
android:gravity="center"
Run Code Online (Sandbox Code Playgroud)
好吧,万一你使用自定义适配器的东西来膨胀你的gridview内的单行视图,然后尝试将android:gravity ="center"添加到你的single_row的布局..这将有希望的伎俩:)即,
<?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"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivClrBlock"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="@+id/tvClrName"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/color_name"
android:textSize="15sp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
尝试将其更改
android:layout_height="fill_parent"
为:
android:layout_height="wrap_content"
您现在告诉网格视图完全填充其父级的高度和宽度。
| 归档时间: |
|
| 查看次数: |
45042 次 |
| 最近记录: |