花京院 2016-01-26 06:12 采纳率: 0%
浏览 4933

GridLayout动态添加子组件.layout_width和layout_height不起做用.

android:id="@+id/recycler_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/dimen_2X"
android:paddingRight="@dimen/dimen_2X"
android:columnCount="5"
>

             gridLayout.post(new Runnable() {
        @Override
        public void run() {
            int width = (gridLayout.getWidth() - 2 * gridLayout.getPaddingLeft()) / COLUMNCOUNT;
            int height = (int) (width / 0.75f);
            MyLogger.jLog().i(width + ":" + height);
            for (int i = 0; i < FAVORITCOUNT; i++) {
                TextView colorCardView = new TextView(getApplicationContext());
                colorCardView.setBackgroundColor(Color.WHITE );
                GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                colorCardView.setLayoutParams(layoutParams);
                gridLayout.addView(colorCardView);
            }
        }
    });
            需求是在Gridlayout中显示25个子组件.每行5个.每个组件宽高比是3:4.
            在log中已经能获取到宽高98,130.
            但实际显示中,子组件的宽为父布局宽,高为宽的4/3.所有子组件重叠在一起.
            感觉params.width和height完全没起作用.
  • 写回答

2条回答 默认 最新

  • 小坏楠楠 2016-01-26 12:28
    关注

    可以使用 以下方式:
    1.gridview 设置 columsNum
    2.自定义子View(在onMeasure处设置其高为getMeasuredWidth的4/3)
    3.为gridview编写adapter动态加载这些子view

    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码