4

我正在使用 FrameLayout 在屏幕上显示(按需)一些文本。我希望文本位于某个位置,所以我认为 setGravity() 可以完成这项工作......但是不,它似乎对文本的去向没有任何影响(而像 ImageView 这样的其他对象甚至没有这种方法)。

那么首先, TextView.setGravity()到底是做什么用的?(编辑:我现在更好地理解了这一点,谢谢!尽管问题的以下部分仍然不清楚。)

其次,似乎以这种方式更新 FrameLayout 的唯一方法是使用您想要的设置创建一个新的 FrameLayout.LayoutParams 对象,然后使用 setLayoutParams() 方法来应用它。(这似乎会自动更新视图,因此是否需要 requestLayout() 调用?)是否有更简单/更直接的方法来为 FrameLayout 实现这一点......比如说,无需像我现在所做的那样创建新的 LayoutParams 对象?

谢谢!作为参考,下面是一个(工作)代码片段,显示了我如何设置这个 FrameLayout 和 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);
4

2 回答 2

3

tv1.setGravity(Gravity.CENTER);

属于TEXT里面的重力TextView

正如文件所述:

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.

于 2011-09-24T08:38:19.213 回答
3

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);
于 2011-09-24T09:30:34.507 回答