Java中TextView的Android layout_gravity

avi*_*i12 1 android layout-gravity

我想改变的布局重心TextView的
通过 XML,您可以通过android:layout_gravity="value".
我知道为了改变重力本身,你会通过 XML 做android:gravity="value",而在 Java 中你会做textview.setGravity(Gravity.VALUE);
但遗憾的是,没有textview.setLayoutGravity(VALUE);,所以我被卡住了。
谢谢!

Kam*_*med 5

您将需要使用正确的子类。例如,如果您TextView在 a 中FrameLayout,则需要:

FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) textView.getLayoutParams();
params.gravity = Gravity.CENTER;
textView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)

请注意layout_gravitysetLayoutGravity在其父元素中设置当前元素的重力,gravitysetGravity为元素内的当前元素内容设置重力。有关更多详细信息,请参阅下图。

示例 1:

<FrameLayout
    android:layout_width="400dp"
    android:layout_height="400dp">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="Some Text" />

</FrameLayout>

---------------------------------------------
|Some Text                                  |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
---------------------------------------------
Run Code Online (Sandbox Code Playgroud)

示例 2:

<FrameLayout
    android:layout_width="400dp"
    android:layout_height="400dp">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:text="Some Text" />

</FrameLayout>

---------------------------------------------
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|            Some Text                      |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
---------------------------------------------
Run Code Online (Sandbox Code Playgroud)

示例 3:

<FrameLayout
    android:layout_width="400dp"
    android:layout_height="400dp">

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Some Text" />

</FrameLayout>

---------------------------------------------
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                 Some Text                 |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
---------------------------------------------
Run Code Online (Sandbox Code Playgroud)

示例 4:

<FrameLayout
    android:layout_width="400dp"
    android:layout_height="400dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Some Text" />

</FrameLayout>

---------------------------------------------
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                 Some Text                 |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
---------------------------------------------
Run Code Online (Sandbox Code Playgroud)

示例 5:

<FrameLayout
    android:layout_width="400dp"
    android:layout_height="400dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Some Text" />

</FrameLayout>

---------------------------------------------
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                 Some Text                 |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
|                                           |
---------------------------------------------
Run Code Online (Sandbox Code Playgroud)