如何对齐文本的Andr​​oid垂直居中 [英] how to align text vertically center in android

查看:104
本文介绍了如何对齐文本的Andr​​oid垂直居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有阿拉伯文字,所以我才能开始从右侧的文本设置重力权利。文字始于现在。但另一个问题是,文本开始从页面顶部渲染。但我需要垂直居中的文本。 虽然我尝试了一些变化,我不能让它垂直居中。

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.

下面是我的xml文件的样本。

Here is the sample of my xml file.

<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>

问题是与上面的TextView。

Problem is with above textview.

在这里,我已经把整个XML文件。

Here I have put whole xml file.

<?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.

。由此我也附加审核后更新code答案。

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

推荐答案

您的TextView属性需要是这样的,

Your TextView Attributes need to be something like,

<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"

让你的TextView到 match_parent FILL_PARENT 如果你不希望它像, match_parent 你必须给一些指定的值 layout_height 因此获得垂直重心的空间。 Android的:layout_width =match_parent必要的,因为它对准右侧的TextView的,所以你可以识别对于TextView中的父布局

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的调整内容。 安卓layout_gravity 使的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.

更新:

正如下面的评论说,使用 FILL_PARENT ,而不是 match_parent 的。 (问题的一些设备。)

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

这篇关于如何对齐文本的Andr​​oid垂直居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆