设置 HorizontalGridView Android 中的列数和水平方向

4

我有一个问题,如标题所述,当我尝试创建可水平滚动的网格视图时,使用 android.support.v17.leanback.widget.HorizontalGridView

结果看起来像这样: enter image description here

但是我需要在每个项之间增加一些空间,并控制屏幕上显示的项数,就像这样: enter image description here

我的 layout.xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical"
tools:context="layout.productListFragment">

<!-- TODO: Update blank fragment layout -->

<android.support.v17.leanback.widget.HorizontalGridView       xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/productListGrid"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="top"   
/>

我尝试使用以下代码,但似乎完全没有起作用。
android: android:numColumns="3"
android:columnWidth="60dp"
android:stretchMode="columnWidth"

这里是single_product_layout的代码。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingLeft="5dp"
android:paddingRight="5dp">

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/singleProduct_image"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:layout_gravity="top"
        android:layout_span="2"
        android:src="@drawable/product1" />
</TableRow>

<TableRow>

    <TextView
        android:id="@+id/singleProduct_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/singleProduct_image"
        android:gravity="left"
        android:text="Tên sản phẩm"
        android:textSize="12dp"
        android:textStyle="bold" />
</TableRow>

<TableRow>

    <RatingBar
        android:id="@+id/singpleProduct_ratingBar"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/singleProduct_name"
        android:layout_gravity="left"
        android:layout_weight="4"
        android:numStars="5" />

    <TextView
        android:id="@+id/singleProduct_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/singleProduct_image"
        android:layout_below="@+id/singleProduct_name"
        android:layout_gravity="right"
        android:layout_weight="6"
        android:gravity="right"
        android:text="25.000VND"
        android:textColor="@color/mainThemeColor"
        android:textSize="10dp" />
</TableRow>

</TableLayout>

非常感谢你。
1个回答

2
我今天刚刚发现,Android Studio(此时版本为2.3)在使用Leanback v17的HorizontalGridView时没有显示所有的自动完成选项。
你需要做的是:
android:horizontalSpacing="10dp"
或者你可以通过编程方式实现:
gridView.setHorizontalSpacing(10)
其中10是像素间距。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接