Android布局参数仅更改宽度和高度

Android布局参数仅更改宽度和高度,android,view,params,Android,View,Params,我知道如何通过执行以下操作,使用LayoutParams设置视图的宽度和高度: android.view.ViewGroup.LayoutParams params = button.getLayoutParams(); params.width = height/7; params.height = height/10; button.setLayoutParams(params); 现在,只要我想对另一个按钮应用相同的高度和宽度,我需要创建另一个LayoutParams或覆盖现有的Layo

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

android.view.ViewGroup.LayoutParams params = button.getLayoutParams();
params.width = height/7;
params.height = height/10;
button.setLayoutParams(params);
现在,只要我想对另一个按钮应用相同的高度和宽度,我需要创建另一个LayoutParams或覆盖现有的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[...]
listOf(button, button2, button3).forEach { b ->
    b.alterLayoutParams {
        it.width = height / 7
        it.height = height / 10
    }
}

提前非常感谢。

使用方法
getLayoutParams()
的全部目的是从
视图中获取现有的(保留所有规则和内容)。然后可以修改那些
LayoutParams
的特定参数,并保持其余参数不变。因此,如果您的
视图在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);

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

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

正如其他答案所说:您需要
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
}
fun View.layoutParams(run: ViewGroup.LayoutParams.() -> Unit) {
    val params = layoutParams
    run(params)
    layoutParams = params
}
如果要将相同的布局参数应用于多个
视图
s,可以执行以下操作:

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[...]
listOf(button, button2, button3).forEach { b ->
    b.alterLayoutParams {
        it.width = height / 7
        it.height = height / 10
    }
}

PS.科特林简直太棒了。

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

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

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

如果只想更改一个参数:

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

所有按钮的宽度和高度都相同吗?宽度=高度,但是高度(按钮)=高度(按钮+1)和宽度(按钮)=宽度(按钮+1)是所有按钮的高度吗?还是只有两个人?都是。对不起,这是数学描述;无论你看哪个按钮,下一个按钮都有相同的参数。你在创建按钮吗?通过编程还是使用布局XML文件?这很好,尽管我更喜欢扩展函数,因为它可以自动执行setLayoutParams。(我将发布另一个答案)似乎不起作用这确实起作用,而且相当优雅。更新:这现在是一个