android - 仅在一行中显示 GridView 中的元素

标签 android gridview

我只想在一行中显示 GridView 元素,这意味着我想在 GridView 中水平滚动。每次我只想在一行中只显示 5 个元素。为此请给我一些建议。在此先感谢。 下面是我的布局代码

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<ImageView 
android:id="@+id/ImageViewlarge" 
android:layout_width="wrap_content" android:layout_height="wrap_content">
</ImageView>
<TextView 

 android:id="@+id/TextViewImageName"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_gravity="center_horizontal" 
  android:editable="false">
  </TextView>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal" 
android:gravity="bottom" android:layout_height="fill_parent">
<ImageView
                       android:id="@+id/ImageViewLeft"
                       android:clickable="true"
                       android:focusable="true"
                       android:layout_alignParentLeft="true" 
                        android:src="@drawable/leftarrow" android:layout_height="50sp" android:layout_width="30sp" android:layout_gravity="bottom|left">
       </ImageView>
<GridView 
android:id="@+id/GridView01"
 android:scrollbars="horizontal" 
    android:layout_height="wrap_content" 
    android:fadingEdge="horizontal" 
    android:scrollbarAlwaysDrawHorizontalTrack="true"
     android:scrollbarAlwaysDrawVerticalTrack="false" 
     android:numColumns="5" android:layout_gravity="center_horizontal" android:layout_width="260sp">
 </GridView>
 <ImageView 
                     android:id="@+id/ImageViewRight" 
                     android:clickable="true"
                     android:focusable="true"                   
                     android:src="@drawable/rightarrow" android:layout_width="30sp" android:layout_height="50sp" android:layout_gravity="right"/>
 </LinearLayout>
 </LinearLayout>

在上面的代码中,现在我想删除那 2 个 ImageView 并将水平滚动设置为该 GridView 元素。为此请给我一些建议,提前致谢

最佳答案

我相信使用 LinearLayout 并将方向设置为水平也可以达到相同的效果。 LinearLayout 可以嵌入 HorizontalScrollView

关于 HorizontalScrollView 的文档实际上是这样说的:

A HorizontalScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a horizontal orientation, presenting a horizontal array of top-level items that the user can scroll through.

关于android - 仅在一行中显示 GridView 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3359511/

相关文章:

java - 该对象内的对象的数组列表

java - Gridview重复图像但不重复android中的文本

javascript - 如果 DropDownList 选定索引已更改,则隐藏 gridview 行

java - Arraylist 将最后一个元素读取为倒数第二个元素

Android:在 webvieww 中更改链接颜色

android - ReceiverRestrictedContext 无法转换为 android.app.Activity

java - 允许用户更改我的应用程序的颜色

android - 更改为Gridview应用程序现在会崩溃?

android - 如何在屏幕中央布局 'grid' 图片

c# - Xamarin 进度对话框未按预期显示