Android自定義GridView顯示一行,並且可以左右滑動


最近做一個類似滑動菜單欄的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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM