【问题标题】:how to get columncount in gridview如何在gridview中获取列数
【发布时间】:2014-08-18 05:08:08
【问题描述】:

gridview noofColumn="autofit"模式下如何在Gridview中获取列数

如何获取列的单行编号

<GridView
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"

                android:background="@android:color/white"
                android:gravity="center"
                android:numColumns="auto_fit"
                android:stretchMode="columnWidth" />

【问题讨论】:

  • 在网格视图“自动调整”模式下,将考虑在移动模式下 2 列,在横向模式下考虑 3 列。

标签: android gridview


【解决方案1】:

您可以在 java 代码中获取列数..

GridView mGridView = (GridView)findViewById(R.id.gridview);
int column_coutnt = mGridView.getNumColumns();

第二种方式:

int columns = 0;
int children = mGridView.getChildCount();
if (children > 0) {
     int width = mGridView.getChildAt(0).getMeasuredWidth();
     if (width > 0) {
        columns = mGridView.getWidth() / width;
     }
 }            

【讨论】:

【解决方案2】:

像这样试试..

Math.ceil( (double)gridView.getCount() / (double)gridView.getNumColumns() );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多