以编程方式设置gridview列宽度不起作用

时间:2023-01-22 22:39:53

I was having some problem when trying to set the grid view column width programmatically. My grid view has two columns and I wanted to set the column width specifically instead of the equal size. Here is my .xml file:

尝试以编程方式设置网格视图列宽时出现问题。我的网格视图有两列,我想专门设置列宽而不是相等的大小。这是我的.xml文件:

<GridView
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
/>

And my code to check for the position to set the width programmatically:

我的代码检查以编程方式设置宽度的位置:

 adapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, items) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView cell = (TextView) super.getView(position, convertView, parent);

            if (position % 2 == 0) {
                cell.setLayoutParams(new LinearLayout.LayoutParams(200, LinearLayout.LayoutParams.WRAP_CONTENT, (float) 0.2));
            } else {
                cell.setLayoutParams(new LinearLayout.LayoutParams(700, LinearLayout.LayoutParams.WRAP_CONTENT, (float) 0.7));
            }
            return cell;
        }

However, it becomes like this. The second column does not expand to fit the full width although I already set the width to 700.

但是,它变成了这样。虽然我已经将宽度设置为700,但第二列不会扩展以适应整个宽度。

Any ideas? Thanks in advanced!

有任何想法吗?先谢谢了!

以编程方式设置gridview列宽度不起作用

1 个解决方案

#1


0  

Try to used Recyclerview it only add one line you can make one or more column in equal size..

尝试使用Recyclerview它只添加一行,你可以制作一个或多个相同大小的列。

  /**
 * this method initialized view control.
 */
private void initView() {
    mRvData = findViewById(R.id.amRvData);


    // below code set layout and scrolling in vertical
   // mRvData.setLayoutManager(new LinearLayoutManager(this));

    // if you want to scroll as horizontal way used below code.
    // mRvData.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

    // if you want to show grid layout used below code
     mRvData.setLayoutManager(new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false));

    mRvData.setItemAnimator(new DefaultItemAnimator());
    mRvData.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

}

refer this link Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)

引用此链接简单的Android网格示例使用RecyclerView和GridLayoutManager(就像旧的GridView一样)

https://www.learn2crack.com/2016/03/grid-recyclerview-with-images-and-text.html

https://www.learn2crack.com/2016/03/grid-recyclerview-with-images-and-text.html

#1


0  

Try to used Recyclerview it only add one line you can make one or more column in equal size..

尝试使用Recyclerview它只添加一行,你可以制作一个或多个相同大小的列。

  /**
 * this method initialized view control.
 */
private void initView() {
    mRvData = findViewById(R.id.amRvData);


    // below code set layout and scrolling in vertical
   // mRvData.setLayoutManager(new LinearLayoutManager(this));

    // if you want to scroll as horizontal way used below code.
    // mRvData.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));

    // if you want to show grid layout used below code
     mRvData.setLayoutManager(new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false));

    mRvData.setItemAnimator(new DefaultItemAnimator());
    mRvData.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

}

refer this link Simple Android grid example using RecyclerView with GridLayoutManager (like the old GridView)

引用此链接简单的Android网格示例使用RecyclerView和GridLayoutManager(就像旧的GridView一样)

https://www.learn2crack.com/2016/03/grid-recyclerview-with-images-and-text.html

https://www.learn2crack.com/2016/03/grid-recyclerview-with-images-and-text.html