简体   繁体   English

如何获取以ImageView为中心的textview

[英]How to get a textview centred on top of an ImageView

I am attempting to get some text to appear in the centre of an ImageView . 我正在尝试使一些文本出现在ImageView的中心。 But using this xml (below) what I see its the text wedged in the top left of the FrameLayout . 但是,使用此xml(在下面),我看到的文本楔入了FrameLayout左上方。 What did I do wrong? 我做错了什么?

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
  >

        <ImageView
            android:id="@+id/levbut"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:scaleType="centerInside"
            android:src="@drawable/starbut"
             />

    <TextView
        android:id="@+id/tvtest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        />

</FrameLayout>

You can either add a gravity to the FrameLayout, a layout_gravity to the textView, or change it to a RelativeLayout and add centerInParent="true". 您可以将重力添加到FrameLayout中,将layout_gravity添加到textView中,或者将其更改为RelativeLayout并添加centerInParent =“ true”。

<FrameLayout
    ...
    android:gravity="center">

or 要么

<FrameLayout
    ... />
    <TextView
        ...
        android:layout_gravity="center" />
</FrameLayout>

or 要么

<ReltiveLayout
    ...>
    <ImageView ... />
    <TextView
        ...
        android:layout_centerInParent="true" />
</RelativeLayout>

FrameLayout元素的android:layout_gravity="center"更改为android:gravity="center"

Try this add android:gravity="center" for TextView and change its width and height to Match parent 试试这个为TextView添加android:gravity =“ center”并将其宽度和高度更改为Match parent

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

<ImageView
    android:id="@+id/levbut"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:scaleType="centerInside"
    android:src="@drawable/star_but" />

<TextView
    android:id="@+id/tvtest"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hello" 
    android:gravity="center"/>

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

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