简体   繁体   English

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

[英]Android - Center Textview in ImageView of Relative Layout

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. 我有一个带有ImageView的相对布局,并且想要在活动布局xml文件中将TextView放在ImageView的中心。

Here is the image view and then what I have tried for the TextView 这是图像视图,然后是我为TextView尝试的图像

This is inside of the relative layout: 这是相对布局的内部:

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

It looks okay I think but the marginTop is 65 shouldn't it be half of the height of the imageView? 我认为看起来不错,但marginTop为65难道不应该是imageView高度的一半吗?

Thanks for the help in advance. 我在这里先向您的帮助表示感谢。

It's not working because you're telling the TextView to be in the center of the parent RelativeLayout, but the ImageView is attached to the top-left of the parent. 它不起作用,因为您告诉TextView位于父级RelativeLayout的中心,但是ImageView附加在父级的左上角。

One approach would be to force the TextView to be the exact size of the ImageView using RelativeLayout's alignXyz properties. 一种方法是使用RelativeLayout的alignXyz属性将TextView强制为ImageView的确切大小。 You then set the gravity attribute to center, and that makes the text appear in the center of the TextView's bounds. 然后,将gravity属性设置为center,这将使文本出现在TextView边界的中心。 This should work as long as the ImageView is larger than the TextView in all dimensions. 只要在所有维度上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"/>

Will that work for you? 那对你有用吗?

One problem I see is this line: 我看到的一个问题是这一行:

android:layout_centerInParent="@+id/logoBackground"

The value of layout_centerInParent should be true or false. layout_centerInParent的值应为true或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). 如果将其设置为true,它将自身居中在父级的中间(相对布局)。 To center it in the ImageView you would need to have the relative layout the same size as the ImageView. 要将其在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>

Try this to your textview : android:layout_centerInParent="true" 试试这个到您的textview: android:layout_centerInParent="true"

So that makes: 这样就可以:

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

Try this code : 试试这个代码:

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

    <ImageView
        android:scaleType="centerCrop"
        android:layout_width="30dp"
        android:layout_height="30dp"/>

    <TextView
        android:id="@+id/your_text_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="1" />
</RelativeLayout>

For this purpose you have to use the Frame Layout . 为此,您必须使用“ Frame Layout

For Reference, See here www.developer.android.com/reference/android/widget/FrameLayout.html 供参考,请参见此处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>

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

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