4
Problem
I’m working on a UI library for android, where I set the layout parameters (height, width, weight and gravity) dynamically via code, in an object LinearLayout.LayoutParams but the attribute gravity does not seem to have the same effect as setGravity(gravity) right in the View.
Note: not directly to
Viewbecause in the coding of the component I do not want to have access toViewdirectly.
What I do briefly is something similar to this:
// width = 0, height = LinearLayout.LayoutParams.WRAP_CONTENT, weight = 1000
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1000);
params.gravity = Gravity.RIGHT;
textView1.setLayoutParams(params);
But the textview1 is not aligned right as it should be.
Already if I make the following code then it works:
// width = 0, height = LinearLayout.LayoutParams.WRAP_CONTENT, weight = 1000
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1000);
textView1.setLayoutParams(params);
textView1.setGravity(Gravity.RIGHT);
Question?
- Why does this happen?
gravityof the objectLinearLayout.LayoutParamsandsetGravityofVieware different frictions?- What is the difference between the two approaches?
- Is there any way to get the same result from
setGravityofViewin thegravityof the objectLinearLayout.LayoutParams?
Obs: A
Viewwhich I refer to in question, in this case is theTextView.To be clear, I made one example in
HTMLof the effect I want onlayoutof myTextViewandroid, through theLinearLayout.LayoutParams.
They’re different attributes, aren’t they? One seems to me to be the gravity of the
LinearLayout, and the other the severity ofTextViewinside theLinearLayout.– Piovezan
In fact I do not know, at first I believed it was the same thing, but it should not be, and if so, there is how to set the gravity of the
TextViewthrough theLinearLayout.LayoutParams?– Fernando Leal
What does the
textview1to be right aligned?TextViewaligned with theLinearLayoutwhich contains it, or is the text within theTextViewthat is aligned to the right?– Piovezan
@Piovezan, it’s the right-aligned text, it’s like a grid I’m doing, so numerical values I want to leave aligned on the right and strings on the left.
– Fernando Leal
So I guess there’s no way. Because the
gravityofLinearLayout.LayoutParamsrefers to the layout within which theTextView. To align the text contained inTextView, you have to change the Gravity of ownTextViewas you are doing in the second example. Why is this second example not feasible for you?– Piovezan
@Piovezan, is that the current structure, is not providing the
Viewto be set directly, only theLinearLayout.LayoutParams, that is set to it. I thought that maybe I could achieve the expected result without having to directly set theView. More is possible yes, with some structure change in the component, if the only solution is this and I will.– Fernando Leal