Android FrameLayout and TextView - why does LayoutParams work where setGravity() does not?
I'm using a FrameLayout to display (on demand) some text on the screen. I want the text to be in a certain place, so I thought setGravity() would do the job... but no, it seems开发者_C百科 to have no effect whatsoever on where the text goes (and other objects like ImageView don't even have this method).
So first, what exactly is TextView.setGravity() used for? (Edit: I understand this much better now, thanks! Still not clear on the following part of the question though.)
Second, it seems the only way to update a FrameLayout in this way is to create a new FrameLayout.LayoutParams object with the settings you want, and then use the setLayoutParams() method to apply it. (This seems to automatically update the view so is a requestLayout() call necessary?) And is there a simpler / more straightforward way to achieve this for a FrameLayout... say, without creating a new LayoutParams object as I'm doing now?
Thanks! For reference, below is a (working) code snippet showing how I'm setting up this FrameLayout and TextView.
FrameLayout fl1 = new FrameLayout(this);
FrameLayout.LayoutParams flp1 = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
fl1.setId(9001);
fl1.setLayoutParams(flp1);
.....
tv1 = new TextView(this);
FrameLayout.LayoutParams tvp1 = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
tv1.setId(9006);
tv1.setLayoutParams(tvp1); // This works
tv1.setBackgroundColor(Color.GRAY);
tv1.setTextColor(Color.BLACK);
tv1.setText("Dynamic layouts ftw!");
tv1.setGravity(Gravity.CENTER); // This does NOT work
.....
fl1.addView(tv1);
1) view.setGravity means hows the view should positions if children. In the case of the textview it refers to the positioning of the text. When in linearlayouts or viewgroups it refers to its child views.
2) I checked your code. You are already using textview.setGravity method. In that case you dont need to specify gravity parameters in the FrameLayout.LayoutParams constructor.
Other thing I noticed is that you gave the textview the width and height as wrap content which will only take the size of the text. So there is no meaning in giving gravity as the textview has no extra area to position the text to the center. You need to give the width of the textview as fill_parent. That should make your gravity property work.
Btw this is a very good article about Gravities. It explains both about gravity and the layout_gravity attribute.
If you want your textview to wrap the content then you should add your textview to a linearlayout and setgravity to the linear layout.
This should give what your are trying to do
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
FrameLayout fr = new FrameLayout(this);
fr.setLayoutParams(lp);
fr.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(lp2);
l.setGravity(Gravity.CENTER);
l.setBackgroundColor(Color.BLUE);
ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView t = new TextView(this);
t.setLayoutParams(vp);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);
l.addView(t);
fr.addView(l);
tv1.setGravity(Gravity.CENTER);
belongs to the gravity of the TEXT inside the TextView
.
As documentation states:
Sets the horizontal alignment of the text and the vertical gravity that will be used when there is extra space in the TextView beyond what is required for the text itself.
精彩评论