我如何在Android上将TextView居中对齐?

10 浏览
0 Comments

我如何在Android上将TextView居中对齐?

我有一个简单的布局,只包含一个EditText和一个TextView


        
             
             
        

我想将TextView居中显示在手机屏幕的中间。

我看到可以将LinearLayout替换为全局布局的RelativeLayout,并设置属性android:gravity="center"来居中显示,但我只想TextView居中,而不是同时将EditText居中。使用此功能时,EditText也会居中显示。

是否可以只将TextView居中而不居中EditText

注意:当然,我不想手动使用边距将其居中。

编辑:我希望TextView在屏幕上居中显示,EditText在下一行显示(在屏幕左侧)。类似于这样:

[                屏幕宽度                ]
                 [TEXTVIEW-居中]
[EDITTEXT-居左]

提前致谢!

0
0 Comments

问题的出现原因:忘记将TextView所在的LinearLayout垂直居中对齐。

解决方法:在LinearLayout的属性中添加android:gravity="center_vertical"。下面是修改后的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="/hello_world" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />
</LinearLayout>

0
0 Comments

The problem is that the TextView is not centered on the screen. The user attempted to solve this issue by changing the orientation of the LinearLayout from horizontal to vertical and adding the attribute android:layout_gravity="center" to the TextView.

However, even after making these changes, the TextView is still not centered on the screen. The user then realizes that they forgot to mention in their question that there is a LinearLayout wrapping both the EditText and the TextView. They decide to edit their question to include this information.

Another user points out that the parent LinearLayout has a width of wrap_content. This means that it will center itself in as small a width as required to fit the children. This is why the TextView is still not centered on the screen.

To fix this issue, the user needs to change the width of the parent LinearLayout to match_parent or a specific width value. This will allow the TextView to be centered properly on the screen.

In conclusion, to center a TextView on Android, you need to change the orientation of the parent LinearLayout to vertical, add the attribute android:layout_gravity="center" to the TextView, and make sure that the width of the parent LinearLayout is set to match_parent or a specific width value.

0
0 Comments

问题的出现原因:在Android中,如何将TextView居中显示。

解决方法:将内部的LinearLayout更改为以下内容:


这样,它将占据整个屏幕空间,然后EditText将被显示出来。如果在内部的LinearLayout中使用wrap_content,则内部LinearLayout的宽度将根据其TextView进行包裹,即它将作为TextView的父布局,但作为外部布局的子布局。如果将其属性更改为fill_parent,它将根据其父布局填充宽度。

fill_parent的意思是填充父布局,即具有fill_parent属性的布局将尝试填充其父布局,而wrap_content将尝试包裹其内容,即在您的情况下,您的TextView大小较小,它尝试包裹其布局。

如果您仍然不理解,请参考此链接中的解释:stackoverflow.com/questions/432763/...

请注意,使用match_parent代替fill_parent,因为fill_parent已被弃用。

对于wrap_content,它总是显示在屏幕的左侧吗?我的意思是,TextView可以像内容一样大(进行包裹),但不能居中显示吗?请注意,TextView也位于内部LinearLayout中。

0