如何获得围绕一个ImageView的顶部一个TextView [英] How to get a textview centred on top of an ImageView

查看:313
本文介绍了如何获得围绕一个ImageView的顶部一个TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一些文字出现在的ImageView 的中心。但是,使用此XML(下)我所看到的文字在的FrameLayout 的左上角楔形。我做了什么错了?

 <的FrameLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:layout_weight =1
  >        < ImageView的
            机器人:ID =@ + ID / levbut
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =match_parent
            机器人:scaleType =centerInside
            机器人:SRC =@绘制/ starbut
             />    <的TextView
        机器人:ID =@ + ID / tvtest
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=你好
        />< /&的FrameLayout GT;


解决方案

您可以重力添加到的FrameLayout,一个layout_gravity到TextView的,或将其更改为RelativeLayout的,并添加centerInParent =真。

 <的FrameLayout
    ...
    机器人:比重=中心>

 <的FrameLayout
    ... />
    <的TextView
        ...
        机器人:layout_gravity =中心/>
< /&的FrameLayout GT;

 < ReltiveLayout
    ...>
    < ImageView的... />
    <的TextView
        ...
        机器人:layout_centerInParent =真/>
< / RelativeLayout的>

I am attempting to get some text to appear in the centre of an ImageView. But using this xml (below) what I see its the text wedged in the top left of the 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
    ...
    android:gravity="center">

or

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

or

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

这篇关于如何获得围绕一个ImageView的顶部一个TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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