Why does the Gravity attribute of the Linearlayout.Layoutparams object not work like the View setGravity() method?

Asked

Viewed 1,170 times

4

Problem

I’m working on a UI library for , 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 to View 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 object LinearLayout.LayoutParams and setGravity of View are different frictions?
  • What is the difference between the two approaches?
  • Is there any way to get the same result from setGravity of View in the gravity of the object LinearLayout.LayoutParams?

Obs: A View which I refer to in question, in this case is the TextView.

To be clear, I made one example in HTML of the effect I want on layout of my TextView , through the LinearLayout.LayoutParams.

  • They’re different attributes, aren’t they? One seems to me to be the gravity of the LinearLayout, and the other the severity of TextView inside the LinearLayout.

  • 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 the LinearLayout.LayoutParams?

  • What does the textview1 to be right aligned? TextView aligned with the LinearLayout which contains it, or is the text within the TextView that is aligned to the right?

  • @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.

  • So I guess there’s no way. Because the gravity of LinearLayout.LayoutParams refers to the layout within which the TextView. To align the text contained in TextView, you have to change the Gravity of own TextView as you are doing in the second example. Why is this second example not feasible for you?

  • @Piovezan, is that the current structure, is not providing the View to be set directly, only the LinearLayout.LayoutParams, that is set to it. I thought that maybe I could achieve the expected result without having to directly set the View. More is possible yes, with some structure change in the component, if the only solution is this and I will.

Show 1 more comment

2 answers

3


  • Why does this happen?
  • gravity of the object LinearLayout.LayoutParams and setGravity of View sane different atribites?
  • What is the difference between the two approaches?

Because in class LinearLayout.LayoutParams the parameter public int gravity refers to gravity for the View in which the object (in its case a TextView) is associated , different from the internal gravity of the object TextView which, in your second example, you used the setGravity(Gravity.RIGHT) that will cause the text of TextView line up on the right.

  • Is there any way to get the same result from setGravity of View in the gravity of the object LinearLayout.LayoutParams?

No, if that "View" that you refer to is the TextView and the result you need is the alignment of the text of the TextView left or right. Since the LinearLayout.LayoutParams is responsible for the information for child layout of the scheme associated with ViewLinearLayout and not the internal gravity of these children.

Sources consulted: Linearlayout.Layoutparams and Textview

  • Your answer, it seems to be interesting and coherent, I’ve been researching the subject and it seems that gravity of LinearLayout.LayoutParams is equivalent to layout_gravity of the [tag:xml], and setGravity() would be equivalent to gravity [tag:xml], making sense of what you quoted.

  • It would be more accurate to say that the parameter gravity of LinearLayout.LayoutParams refers to the severity of the layout within which the TextView, thus affecting the positioning of TextView within that layout and not what exists within the TextView.

0

But I didn’t understand if you want to line up the text right inside the textview or if you want to align yourself texview to the right. Sorry for the reply but I have no reputation for comment...

I can answer that depending on what you want, if you can explain it better...

Answer

You can’t do that through LinearLayout.LayoutParams. You can do something else, define 2 types of TextView in the , one with text aligned to the right another with text aligned to the left, so when you want to use it just use the one that suits you most.

  • Align the text to the right. I made a example in [tag:html] of the effect I want on layout of my TextView [tag:android], the way I quoted in the question, through the LinearLayout.LayoutParams

  • You can’t do that through LinearLayout.LayoutParams. You can do something else, define 2 types of TextView in xml, one with text aligned to the right another with text aligned to the left, so when you want to use it just use the one that suits you most.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.