问题
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);
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);
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[...]
Thanks a lot in advance.
回答1:
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.
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);
回答2:
In Kotlin we can do it shorter and more elegant:
view.layoutParams = view.layoutParams.apply {
width = LayoutParams.MATCH_PARENT
height = LayoutParams.WRAP_CONTENT
}
回答3:
As other answers have said: you need to getLayoutParams()
, alter the returned LayoutParams object, and setLayoutParams()
with it (whether this is a reasonable API is up for debate).
But as comm1x says, Kotlin can make this really simple, and easy to read.
How about this:
button.alterLayoutParams {
it.width = height / 7
it.height = height / 10
}
This requires an extension function:
private fun View.alterLayoutParams(callback: ((ViewGroup.LayoutParams) -> Unit)) {
val p = this.layoutParams
callback(p)
this.layoutParams = p
}
If you wanted to apply the same layout parameters to multiple View
s, you can do something like:
listOf(button, button2, button3).forEach { b ->
b.alterLayoutParams {
it.width = height / 7
it.height = height / 10
}
}
PS. Kotlin is nothing short of fantastic.
回答4:
As I cannot comment on others yet: I created a small and elegant extension function for Kotlin.
fun <T: ViewGroup.LayoutParams>View.layoutParams(run: T.() -> Unit) {
val params = layoutParams as T
run(params)
layoutParams = params
}
A use case for a view in a constraintLayout for instance would be:
view.layoutParams<ConstraintLayout.LayoutParams> {
width = LayoutParams.WRAP_CONTENT
height = LayoutParams.WRAP_CONTENT
}
If you don't need a specific implementation of LayoutParams (it is advised to do use a specific one), you can use a smaller version of the extension function:
fun View.layoutParams(run: ViewGroup.LayoutParams.() -> Unit) {
val params = layoutParams
run(params)
layoutParams = params
}
..which would result in:
view.layoutParams {
width = LayoutParams.WRAP_CONTENT
height = LayoutParams.WRAP_CONTENT
}
来源:https://stackoverflow.com/questions/28269837/android-layout-params-change-only-width-and-height