Android - 相对布局的ImageView中的中心Textview

时间:2014-12-06 01:41:13

标签: java android xml android-layout

我有一个带有ImageView的相对布局,并希望将TextView放在ImageView的一侧,在活动布局xml文件中。

这是图像视图,然后是我为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高度的一半吗?

感谢您的帮助。

4 个答案:

答案 0 :(得分:2)

它无效,因为您告诉TextView位于父RelativeLayout的中心,但ImageView附加到父级的左上角。

一种方法是使用RelativeLayout的alignXyz属性强制TextView成为ImageView的确切大小。然后,您将gravity属性设置为居中,这使得文本显示在TextView边界的中心。只要ImageView在所有维度上都大于TextView,这应该可以正常工作。

<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_alignRight="@+id/logoBackground"
    android:layout_alignBottom="@+id/logoBackground"
    android:gravity="center"/>

这对你有用吗?

答案 1 :(得分:2)

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

android:layout_centerInParent="@+id/logoBackground"

layout_centerInParent的值应为true或false。我猜测它正在评估为假,位于左上角。如果将其设置为true,它将自身居中于父级的中间(相对布局)。要使其在ImageView中居中,您需要使相对布局与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>

答案 2 :(得分:1)

尝试使用textview:android:layout_centerInParent="true"

这样做:

<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"
    android:layout_centerInParent="true"/>

答案 3 :(得分:0)

为此,您必须使用Frame Layout

参考,请参阅www.developer.android.com/reference/android/widget/FrameLayout.html

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <ImageView 
   android:src="@drawable/ic_launcher"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"/>

   <TextView
   android:text="Frame Text"
   android:textSize="30px"
   android:textStyle="bold"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"
   android:gravity="center"/>
</FrameLayout>
相关问题