简体   繁体   English

将TextView居中:为什么不起作用?

[英]Center a TextView: Why it doesn't work?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textColor="@color/red"
    android:textStyle="bold"
    android:text="@string/desc" 
    />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"
    android:text="Welcome"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

</RelativeLayout>

I would like to center the second TextView but I can not ... This is the code, you know help me? 我想将第二个TextView居中,但是我不能...这是代码,您知道对我有帮助吗? Where is the error? 错误在哪里? I'm going crazy! 我要疯了!

Add the following to your textview: 将以下内容添加到您的textview中:

android:gravity="center"

But at the same time I would recommend to reconsider your layout as you're having 2 textviews taking the full screen. 但是同时,我建议您重新考虑布局,因为您有2个全屏的Textview。

Edit center centers both on horizontal and vertical. 编辑 center在水平和垂直上都center If you need only on a specific axis, use one of: android:gravity="center_horizontal" or android:gravity="center_vertical" 如果仅需要特定轴,请使用以下之一:android:gravity =“ center_horizo​​ntal”或android:gravity =“ center_vertical”

try this : 尝试这个 :

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"
    android:text="Welcome"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

Change your layout like this 像这样更改您的布局

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textColor="@color/red"
    android:textStyle="bold"
    android:text="@string/desc" 
    />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"   
    android:text="Welcome"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

</RelativeLayout>

Always use match_parent instead of fill_parent 始终使用match_parent而不是fill_parent

What about setting it this way 这样设置怎么办

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"        >
    <TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:textColor="@color/red"
    android:textStyle="bold"
    android:text="@string/desc"         />

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="60dp"
    android:textStyle="bold|italic"
    android:text="Welcome"
    android:textAppearance="?android:attr/textAppearanceLarge"         />

</RelativeLayout>

Alternatively 另外

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout2"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:layout_height="fill_parent">
        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_contet"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"           
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textColor="@color/red"
        android:textStyle="bold"
        android:text="@string/desc"             />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"           
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:textStyle="bold|italic"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>
// try this 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginTop="60dp"
        android:textStyle="bold|italic"
        android:text="Welcome"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/textView2"
        android:layout_marginTop="20dp"
        android:textColor="@color/red"
        android:text="@string/desc"
        android:textStyle="bold"/>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM