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
View
because in the coding of the component I do not want to have access toView
directly.
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?
gravity
of the objectLinearLayout.LayoutParams
andsetGravity
ofView
are different frictions?- What is the difference between the two approaches?
- Is there any way to get the same result from
setGravity
ofView
in thegravity
of the objectLinearLayout.LayoutParams
?
Obs: A
View
which I refer to in question, in this case is theTextView
.To be clear, I made one example in
HTML
of the effect I want onlayout
of myTextView
android, 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 ofTextView
inside 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
TextView
through theLinearLayout.LayoutParams
?– Fernando Leal
What does the
textview1
to be right aligned?TextView
aligned with theLinearLayout
which contains it, or is the text within theTextView
that 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
gravity
ofLinearLayout.LayoutParams
refers to the layout within which theTextView
. To align the text contained inTextView
, you have to change the Gravity of ownTextView
as 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
View
to 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