Android布局参数仅更改宽度和高度 [英] Android Layout Params change ONLY width and height

查看:120
本文介绍了Android布局参数仅更改宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to set the width and the height of a view using LayoutParams via doing the following:

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

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

Now, as soon as I wanna apply the same height and the width to another button, I need to create another LayoutParams or overwrite the existing one like here:

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

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

I have about 50 buttons in my application and I doubt it is considered good code to get all the Params in order to only change the width and the height - I want to keep the remaining params (toLeftOf, [...]).

是否有一种方法只能更改参数的宽度和高度,但保留其余参数?因此,它看起来像:

Is there a way to ONLY change the width and the height of the params but keep the rest of the parameters? So it then looks like something like:

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[...]

非常感谢.

推荐答案

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

The whole purpose of using method getLayoutParams() is to take the exisiting ones from a View (that keep all the rules and stuff). Then you can modify specific parameters of those LayoutParams and keep the rest unmodified. Therefore you need to call getLayoutParams() and modify the returned object if your Views vary in settings in xml. In case they were using the exact same rules in xml, you could do it just like you wrote in your last example.

我建议您做的只是做一个包装更新LayoutParams整个过程的方法.像这样:

What I would advice you to do is just to make a method that would wrap the whole process of updating LayoutParams. Like so:

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);
}

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

That would simplify your code significantly, because then you can just call this method for every single of your buttons, with proper values for width and height.

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

这篇关于Android布局参数仅更改宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆