本文介绍了Android的 - 中心TextView的相对布局的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它的ImageView的相对布局,并希望在中心侧的TextView的ImageView的,在活动布局xml文件。

I have a relative layout with an ImageView in it and want to center the TextView in side the ImageView, in the activity layout xml file.

下面是图像视图,然后我什么都试过了的TextView

Here is the image view and then what I have tried for the TextView

这是相对布局内:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="170dp"
    android:minWidth="170dp"
    android:id="@+id/logoBackground"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="33dp"
    android:layout_marginStart="33dp"
    android:layout_marginTop="33dp"
    android:contentDescription="@string/content_description_useless"
    android:background="#ffffd0cd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_alignTop="@+id/logoBackground"
    android:layout_alignLeft="@+id/logoBackground"
    android:layout_alignStart="@+id/logoBackground"
    android:layout_marginLeft="43dp"
    android:layout_marginStart="43dp"
    android:layout_marginTop="65dp"/>

这看起来不错,我认为,但marginTop 65不是它应该是一半的ImageView的高度?

It looks okay I think but the marginTop is 65 shouldn't it be half of the height of the imageView?

感谢您提前帮助。

推荐答案

有一个问题我看到的是这一行:

One problem I see is this line:

android:layout_centerInParent="@+id/logoBackground"

layout_centerInParent的值应该是真的还是假的。我猜测它正在评估为false,并在左上角。如果你把它设置为true,将中心本身在父(相对布局)的中间。要居中它在ImageView的,你需要有相对布局大小与ImageView的一样。

The value of layout_centerInParent should be true or false. I'm guessing it is evaluating to false and is in the upper left corner. If you set it to true it will center itself in the middle of the parent (the relative layout). To center it in the ImageView you would need to have the relative layout the same size as the ImageView.

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="170dp"
    android:minWidth="170dp"
    android:id="@+id/logoBackground"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="33dp"
    android:layout_marginStart="33dp"
    android:layout_marginTop="33dp"
    android:contentDescription="@string/content_description_useless"
    android:background="#ffffd0cd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_centerInParent="true"/>
</RelativeLayout>

这篇关于Android的 - 中心TextView的相对布局的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:06