Why is the gravity attribute of the LinearLayout.LayoutParams object not working like the setGravity () method of View?

4

Problem

I'm working on a UI library to , where hedge the layout parameters ( height , width , weight and gravity ) dynamically via code in a LinearLayout.LayoutParams object but the gravity attribute does not seem to have the same setGravity(gravity) .

  

Note: I do not directly link to View because in component encoding I do not intend to have access to View directly.

What I do briefly is something like 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 View does not align right as it should.

Now 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?
  • textview1 of object gravity and LinearLayout.LayoutParams of setGravity are different attributes?
  • What's the difference between the two approaches?
  • Is there any way to get the same result of View of setGravity in View of object gravity ?
  

Note: The LinearLayout.LayoutParams that I refer to in the question, in this case is View .

     

For clarity, I did a example in% with% of the effect I want on TextView of my HTML through layout .

    
asked by anonymous 27.03.2014 / 21:24

2 answers

3
  
  • Why does this happen?
  •   
  • gravity of object LinearLayout.LayoutParams and setGravity of View are   different tributes?
  •   
  • What's the difference between the two approaches?
  •   

Because in class LinearLayout.LayoutParams the public int gravity parameter refers to the severity of the View in which the object (in this case a TextView ) is associated, other than the internal severity of the object TextView , in your second example, you used the setGravity(Gravity.RIGHT) method that will cause the TextView text to align right.

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

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

Referred sources: LinearLayout.LayoutParams and TextView

    
01.04.2014 / 20:24
0

But I did not realize if you want to align the text right inside textview or if you want to align own texview to the right. Sorry for the answer but I have no reputation to comment ...

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

Response

  

You can not do this with LinearLayout.LayoutParams . You can do something else, you define 2 types of TextView in the , one with text aligned to the right another with the text aligned to the left, so when you want to use just use whatever works for you.

    
03.04.2014 / 13:35