如何在android中垂直对齐文本中心

时间:2022-09-07 10:45:11

I have arabic text, therefore I set gravity to right in order to start text from right side. Text starts from right now. But another issue is text starts to render from the top of the page. But I need to vertically center the text. Although I tried several variations I couldnt make it vertically center.

我有阿拉伯语文本,因此我将重力设为右以便从右边开始。文字从现在开始。另一个问题是文本从页面顶部开始呈现。但是我需要把文字垂直居中。虽然我尝试了几种变型,但我无法使它垂直居中。

Here is the sample of my xml file.

这是我的xml文件的示例。

<LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="23dp"
                android:gravity="right"
                android:padding="@dimen/padding_maintextview"
                android:text="@string/text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20sp" />
        </LinearLayout>

Problem is with above textview.

问题是上面的textview。

Here I have put whole xml file.

这里我放了整个xml文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/page1background"
    android:paddingRight="@dimen/padding_large" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="196dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:paddingTop="@dimen/padding_Title_Top"
        android:text="@string/text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <View
            android:id="@+id/view1"
            android:layout_width="fill_parent"
            android:layout_height="5dp" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/linearLayout2"
        android:layout_below="@id/linearLayout1"
        android:layout_gravity="center"
        android:padding="@dimen/padding_maintextview" >

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="23dp"
                android:gravity="right"
                android:padding="@dimen/padding_maintextview"
                android:text="@string/text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <View
            android:id="@+id/view2"
            android:layout_width="fill_parent"
            android:layout_height="100dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <ImageButton
            android:id="@+id/back_arrow"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp"
            android:layout_marginRight="45dp"
            android:layout_weight=".5"
            android:background="@drawable/backbut"
            android:contentDescription="@string/Description"
            android:onClick="onClickBtn"
            android:src="@drawable/backarrowpress" />

        <ImageButton
            android:id="@+id/copyButton"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45dp"
            android:layout_weight=".5"
            android:background="@drawable/copy"
            android:contentDescription="@string/Description"
            android:onClick="onClickBtn" />
    </LinearLayout>

</RelativeLayout>

Can anybody show me where I have done the mistake? I think problem is clear. If not tell me in comments.

谁能告诉我哪里做错了吗?我认为问题很明显。如果不告诉我的评论。

Herewith I have appended updated code after review your answers.

随函附上更新后的代码,请审阅您的答案。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/page1background"
    android:paddingRight="@dimen/padding_large" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="196dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:paddingTop="@dimen/padding_Title_Top"
        android:text="@string/text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <View
            android:id="@+id/view1"
            android:layout_width="fill_parent"
            android:layout_height="5dp" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/linearLayout2"
        android:layout_below="@id/linearLayout1"
        android:layout_gravity="center"
        android:layout_centerInParent="true"
        android:padding="@dimen/padding_maintextview" >

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="center_vertical"
                android:layout_marginBottom="23dp"
                android:gravity="center_vertical|right"
                android:padding="@dimen/padding_maintextview"
                android:text="@string/text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <View
            android:id="@+id/view2"
            android:layout_width="fill_parent"
            android:layout_height="100dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <ImageButton
            android:id="@+id/back_arrow"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginBottom="30dp"
            android:layout_marginRight="45dp"
            android:layout_weight=".5"
            android:background="@drawable/backbut"
            android:contentDescription="@string/Description"
            android:onClick="onClickBtn"
            android:src="@drawable/backarrowpress" />

        <ImageButton
            android:id="@+id/copyButton"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45dp"
            android:layout_weight=".5"
            android:background="@drawable/copy"
            android:contentDescription="@string/Description"
            android:onClick="onClickBtn" />
    </LinearLayout>

</RelativeLayout>

But I am in same situation. No text is vertically centered

但我的处境相同。没有文本是垂直居中的

5 个解决方案

#1


96  

Your TextView Attributes need to be something like,

你的TextView属性应该是,

<TextView ... 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|right" ../>

Now, Description why these need to be done,

现在,描述为什么这些需要做,

 android:layout_width="match_parent"
 android:layout_height="match_parent"

Makes your TextView to match_parent or fill_parent if You don't want to be it like, match_parent you have to give some specified values to layout_height so it get space for vertical center gravity. android:layout_width="match_parent" necessary because it align your TextView in Right side so you can recognize respect to Parent Layout of TextView.

将TextView设置为match_parent或fill_parent(如果不希望设置为match_parent)则必须为layout_height指定一些指定值,这样才能获得垂直重心的空间。必须使用layout_width="match_parent",因为它将TextView对齐到右侧,这样就可以识别TextView的父布局。

Now, its about android:gravity which makes the content of Your TextView alignment. android:layout_gravity makes alignment of TextView respected to its Parent Layout.

现在,它是关于android:gravity的,它使你的TextView对齐。android:layout_gravity使TextView的对齐方式受其父布局的影响。

Update:

更新:

As below comment says use fill_parent instead of match_parent. (Problem in some device.)

如下注释所示,使用fill_parent而不是match_parent。在一些设备(的问题)。

#2


16  

just use like this to make anything to center

就像这样做任何东西

 android:layout_gravity="center"
 android:gravity="center"

updated :

更新:

android:layout_gravity="center|right"
android:gravity="center|right"

Updated : Just remove MarginBottom from your textview.. Do like this.. for your textView

更新:从你的textview中删除边沿。这样做. .为你的textView

<LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"  >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:gravity="center|right"
            android:text="hello" 
            android:textSize="20dp" />
    </LinearLayout>

#3


8  

Try to put android:gravity="center_vertical|right" inside parent LinearLayout else as you are inside RelativeLayout you can put android:layout_centerInParent="true" inside your scrollView.

试着把android:gravity=“center_vertical|right”放在父线性布局中,当你在RelativeLayout中时,你可以将android:layout_centerInParent=“true”放入scrollView中。

#4


7  

The problem is the padding of the font on the textview. Just add to your textview:

问题是textview中字体的填充。只要添加到你的textview:

android:includeFontPadding="false"

#5


0  

In relative layout you need specify textview height:

在相对布局中,需要指定textview高度:

android:layout_height="100dp"

Or specify lines attribute:

或指定行属性:

android:lines="3"

#1


96  

Your TextView Attributes need to be something like,

你的TextView属性应该是,

<TextView ... 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|right" ../>

Now, Description why these need to be done,

现在,描述为什么这些需要做,

 android:layout_width="match_parent"
 android:layout_height="match_parent"

Makes your TextView to match_parent or fill_parent if You don't want to be it like, match_parent you have to give some specified values to layout_height so it get space for vertical center gravity. android:layout_width="match_parent" necessary because it align your TextView in Right side so you can recognize respect to Parent Layout of TextView.

将TextView设置为match_parent或fill_parent(如果不希望设置为match_parent)则必须为layout_height指定一些指定值,这样才能获得垂直重心的空间。必须使用layout_width="match_parent",因为它将TextView对齐到右侧,这样就可以识别TextView的父布局。

Now, its about android:gravity which makes the content of Your TextView alignment. android:layout_gravity makes alignment of TextView respected to its Parent Layout.

现在,它是关于android:gravity的,它使你的TextView对齐。android:layout_gravity使TextView的对齐方式受其父布局的影响。

Update:

更新:

As below comment says use fill_parent instead of match_parent. (Problem in some device.)

如下注释所示,使用fill_parent而不是match_parent。在一些设备(的问题)。

#2


16  

just use like this to make anything to center

就像这样做任何东西

 android:layout_gravity="center"
 android:gravity="center"

updated :

更新:

android:layout_gravity="center|right"
android:gravity="center|right"

Updated : Just remove MarginBottom from your textview.. Do like this.. for your textView

更新:从你的textview中删除边沿。这样做. .为你的textView

<LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"  >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:gravity="center|right"
            android:text="hello" 
            android:textSize="20dp" />
    </LinearLayout>

#3


8  

Try to put android:gravity="center_vertical|right" inside parent LinearLayout else as you are inside RelativeLayout you can put android:layout_centerInParent="true" inside your scrollView.

试着把android:gravity=“center_vertical|right”放在父线性布局中,当你在RelativeLayout中时,你可以将android:layout_centerInParent=“true”放入scrollView中。

#4


7  

The problem is the padding of the font on the textview. Just add to your textview:

问题是textview中字体的填充。只要添加到你的textview:

android:includeFontPadding="false"

#5


0  

In relative layout you need specify textview height:

在相对布局中,需要指定textview高度:

android:layout_height="100dp"

Or specify lines attribute:

或指定行属性:

android:lines="3"