【问题标题】:Android Layout Params change ONLY width and heightAndroid 布局参数仅更改宽度和高度
【发布时间】:2015-03-31 22:22:57
【问题描述】:

我知道如何使用LayoutParams 通过执行以下操作来设置视图的宽度和高度:

android.view.ViewGroup.LayoutParams params = button.getLayoutParams();
params.width = height/7;
params.height = height/10;
button.setLayoutParams(params);

现在,一旦我想将相同的高度和宽度应用到另一个按钮,我需要创建另一个 LayoutParams 或覆盖现有的,如下所示:

android.view.ViewGroup.LayoutParams params2 = but2.getLayoutParams();
params2.width = height/7;
params2.height = height/10;
but2.setLayoutParams(params2);

我的应用程序中有大约 50 个按钮,我怀疑获取所有参数以便仅更改宽度和高度是否被认为是好的代码 - 我想保留剩余的参数(toLeftOf,[...] )。

有没有办法只改变参数的宽度和高度,但保留其余参数?所以它看起来像这样:

android.view.ViewGroup.LayoutParams params = button.getLayoutParams();
params.width = height/7;
params.height = height/10;
button.setLayoutParams(params);
button2.setLayoutParams(params);
button3.setLayoutParams(params);
butt[...]

非常感谢。

【问题讨论】:

  • 所有按钮的宽度和高度都一样吗?
  • width!=height,但是 height(button)==height(button+1) 和 width(button)==width(button+1)
  • 所有按钮都这样吗?还是只供 2 人使用?
  • 全部。对不起那个数学描述;无论您查看哪个按钮,下一个按钮都具有相同的参数
  • 您是如何创建按钮的?以编程方式还是使用布局 XML 文件?

标签: android view params


【解决方案1】:

使用方法getLayoutParams() 的全部目的是从View 中获取现有的(保留所有规则和内容)。然后你可以修改那些LayoutParams的具体参数,其余的保持不变。因此,如果您的 Views 在 xml 中的设置不同,您需要调用 getLayoutParams() 并修改返回的对象。如果他们在 xml 中使用完全相同的规则,您可以像在上一个示例中编写的那样进行操作。

我建议你做的只是创建一个方法来包装更新LayoutParams 的整个过程。像这样:

private void setDimensions(View view, int width, int height){
    android.view.ViewGroup.LayoutParams params = view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

这将大大简化您的代码,因为您可以为每个按钮调用此方法,并为宽度和高度设置适当的值。

setDimensions(button, height/7, height/10);
setDimensions(button2, height/7, height/10);
setDimensions(button3, height/7, height/10);

【讨论】:

    【解决方案2】:

    Kotlin 中,我们可以做得更短更优雅:

    view.layoutParams = view.layoutParams.apply {
        width = LayoutParams.MATCH_PARENT
        height = LayoutParams.WRAP_CONTENT
    }
    

    【讨论】:

    • 这很好,虽然我更喜欢扩展功能,因为它可以自动执行 setLayoutParams。 (我会发布另一个答案)
    【解决方案3】:

    正如其他答案所说:您需要 getLayoutParams(),更改返回的 LayoutParams 对象,然后 setLayoutParams() 使用它(这是否是一个合理的 API 有待商榷)。

    但正如 comm1x 所说,Kotlin 可以使这变得非常简单,并且易于阅读。

    这个怎么样:

    button.alterLayoutParams { 
        it.width = height / 7
        it.height = height / 10
    }
    

    这需要一个扩展函数:

    private fun View.alterLayoutParams(callback: ((ViewGroup.LayoutParams) -> Unit)) {
        val p = this.layoutParams
        callback(p)
        this.layoutParams = p
    }
    

    如果您想将相同的布局参数应用于多个Views,您可以执行以下操作:

    listOf(button, button2, button3).forEach { b ->
        b.alterLayoutParams {
            it.width = height / 7
            it.height = height / 10
        }
    }
    

    PS。 Kotlin 简直太棒了。

    【讨论】:

      【解决方案4】:

      由于我还不能评论其他人: 我为 Kotlin 创建了一个小巧而优雅的扩展函数。

      fun <T: ViewGroup.LayoutParams>View.layoutParams(run: T.() -> Unit) {
          val params = layoutParams as T
          run(params)
          layoutParams = params
      }
      

      例如,constraintLayout 中的视图的用例是:

      view.layoutParams<ConstraintLayout.LayoutParams> {
          width = LayoutParams.WRAP_CONTENT
          height = LayoutParams.WRAP_CONTENT
      }
      

      如果您不需要 LayoutParams 的特定实现(建议使用特定的),您可以使用较小版本的扩展函数:

      fun View.layoutParams(run: ViewGroup.LayoutParams.() -> Unit) {
          val params = layoutParams
          run(params)
          layoutParams = params
      }
      

      ..这将导致:

      view.layoutParams {
          width = LayoutParams.WRAP_CONTENT
          height = LayoutParams.WRAP_CONTENT
      }
      

      【讨论】:

      【解决方案5】:

      如果你只想改变一个参数:

      view.getLayoutParams().width = 400;
      view.requestLayout();
      

      【讨论】:

        【解决方案6】:
        view.updateLayoutParams {
            width = LayoutParams.WRAP_CONTENT
            height = LayoutParams.WRAP_CONTENT
        }
        

        【讨论】:

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