花京院 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 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的