最近做一个类似滑动菜单栏的title,绑定数据源用的是GrildView,想要实现横着滑动并且GrildView只显示一行。最终采用代码形式在Activity中动态的添加布局实现。

    ViewGroup.LayoutParams params = dishtype.getLayoutParams();
              // dishtype,welist为ArrayList
int dishtypes = welist.size(); params.width = 115 * dishtypes; Log.d("看看这个宽度", params.width+""+welist.size()); dishtype.setLayoutParams(params); //设置列数为得到的list长度
dishtype.setNumColumns(welist.size());

布局文件采用了HorizontalScrollView 这个控件来控制左右滑动。

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="80dp"       
            >
            <LinearLayout
                android:id="@+id/liear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"  
                >
                <GridView
                    android:id="@+id/dishtype"
                    android:layout_width="match_parent"
                    android:layout_height="80dip"
                    android:cacheColorHint="#00000000"
                    android:columnWidth="108dip"
                    android:gravity="center"
                    android:horizontalSpacing="1.0dip"
                    android:listSelector="#00000000"
                    android:numColumns="auto_fit"
                    android:stretchMode="spacingWidthUniform"
                    android:verticalSpacing="1.0dip" >
                </GridView>
            </LinearLayout>
        </FrameLayout>
    </HorizontalScrollView>

 

相关文章:

  • 2021-11-13
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2022-12-23
  • 2022-01-20
  • 2021-05-12
猜你喜欢
  • 2021-12-11
  • 2021-09-08
  • 2021-07-11
  • 2021-08-18
  • 2021-12-22
  • 2021-08-04
  • 2022-12-23
相关资源
相似解决方案