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

查看:32
本文介绍了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 或像这里一样覆盖现有的:

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天全站免登陆