【问题标题】:Android GridLayout layout_columnWeight="1" and layout_width="0dp" programmaticallyAndroid GridLayout layout_columnWeight="1" 和 layout_width="0dp" 以编程方式
【发布时间】:2022-02-07 19:11:28
【问题描述】:

我使用 GridLayout 并希望以编程方式添加 View。

我希望所有视图平分但遇到一些问题。

我的问题:(600 宽度和700 宽度不相等)

我想要:

xml:

  <GridLayout
                        android:id="@+id/withdrawSettingLayout"
                        android:layout_width="match_parent"
                        android:useDefaultMargins="true"
                        android:layout_height="wrap_content"
                        android:columnCount="4"/>

Kotlin 代码:

list.forEach { amount->

    val titleText = TextView(context).apply {
        text = amount
        setTextColor(ContextCompat.getColor(context, R.color.white))
    }
    val layoutParams = GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f), GridLayout.spec(GridLayout.UNDEFINED, 1f))
    layoutParams.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1f)
    viewBinding.withdrawSettingLayout.addView(titleText, layoutParams)
}

如何解决,谢谢。

【问题讨论】:

  • 你试过使用线性布局还是约束布局?
  • @TarsilaCostalonga 是的,它们可以工作,但更复杂。

标签: android kotlin grid-layout


【解决方案1】:

不确定是否有帮助,但您也可以尝试添加:

layoutParams.width = 0

【讨论】:

  • 它有效。简直不敢相信,谢谢!
猜你喜欢
  • 2017-07-14
  • 2016-12-09
  • 2014-04-22
  • 1970-01-01
  • 2017-04-07
  • 2016-11-13
  • 2015-05-03
  • 1970-01-01
  • 2013-05-14
相关资源
最近更新 更多