简体   繁体   English

如何将TextView放在ImageView的顶部

[英]How can I place a TextView on top of ImageView

Is there an easy way to place a TextView on top of ImageView? 有没有一种简单的方法可以将TextView放在ImageView的顶部?

My code: 我的代码:

<ImageView
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:src="@drawable/finaloval"
    android:elevation="10dp"
    android:layout_centerHorizontal="true"
    android:id="@+id/greencircleId"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:id="@+id/textView"
    android:textSize="40sp"
    android:textColor="@color/colorAccent" />

Example Image 范例图片

The green circle would be my @drawable/finaloval. 绿色圆圈是我的@ drawable / finaloval。 And the %100 organic would be the TextView. 而100%的有机字体将是TextView。

Just use a LinearLayout as root and re-sequence your views from top to bottom, meaning in your case should be TextView then ImageView . 只需将LinearLayout用作根,然后将视图从上到下重新排序,这意味着在您的情况下应为TextView然后ImageView

Edited based on comment 根据评论进行编辑

To do overlap of views, you can use RelativeLayout or FrameLayout as suggested by others. 要进行视图重叠,可以使用其他人建议的RelativeLayoutFrameLayout

Try this: 尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_marginTop="23dp"
    android:id="@+id/textView"
    android:textSize="40sp"
    android:textColor="@color/colorAccent" />

<ImageView
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:src="@drawable/finaloval"
    android:elevation="10dp"
    android:id="@+id/greencircleId"/>

</RelativeLayout>

Think this is the simplest way of doing. 认为这是最简单的方法。 Hope this helps 希望这可以帮助

<TextView android:id="@+id/tv_versionstatus"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:drawablePadding="5dp"  
                    android:drawableBottom="@drawable/icon_new"  
                    android:text="text"  
                    android:textColor="#363636"  
                    android:textSize="20sp" />  

drawableBottom is also a choise for you drawableBottom也是您的选择

Please try to set the background for the layout and place your textviews inside that , i have coded it as per the image you shared in it example
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="@drawable/finaloval">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="100% natural"
    android:layout_marginTop="23dp"
    android:id="@+id/natural"
    android:textSize="20sp"
    android:textColor="@color/colorAccent" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="organic"
    android:layout_marginTop="23dp"
    android:layout_below="@id/"
    android:id="@+id/natural"
    android:textSize="40sp"
    android:textColor="@color/colorAccent" />

</RelativeLayout>

try this xml: 试试这个xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@drawable/finaloval"
        android:layout_gravity="center"
        android:elevation="10dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textSize="40sp"
        android:layout_gravity="center"
        android:textColor="@color/text_black" />
</FrameLayout>

Try to set the imageview as the background of the textview. 尝试将imageview设置为textview的背景。

Do you want to put some words upon a image or a sharp drawable? 您要在图像或锐利的可绘制对象上添加一些文字吗?

I'll use a shape drawable to make an example. 我将使用一个可绘制的形状作为示例。

Now I have a shape like this: 现在我有一个这样的形状:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval"
   >

    <size
        android:height="100dp"
        android:width="100dp"
        />

    <solid
        android:color="#66ccff"
        />

</shape>

Then set it as the background of a textview in a layout xml. 然后将其设置为布局xml中textview的背景。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/circle"
    android:text="Hello World!"
    />

That looks like it. 看起来像。 It's a 100dp*100dp one. 这是100dp * 100dp的一个。 100*100 100 * 100

And try to modify the width and height of the textview. 并尝试修改textview的宽度和高度。

<TextView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/circle"
    android:text="Hello World!"
    />

Now it looks like this. 现在看起来像这样。 A Lot Larger. 大得多。 200*200 200 * 200

So the conclusion is: 因此,结论是:

  • Change the size of drawable and use wrap_content in the layout xml. 更改drawable的大小,并在布局xml中使用wrap_content。
  • Change "layout_width" and "layout_height" in the layout xml. 在布局xml中更改“ layout_width”和“ layout_height”。

But if you want to use the smaller one or the lager one, maybe you should add some control codes in the control java file. 但是,如果要使用较小的一个或较大的一个,则应在控制java文件中添加一些控制代码。

(Sorry I'm not in a English-spoken country so maybe my words is not clear enough) (对不起,我不在讲英语的国家,所以也许我的话不够清楚)

I'm sorry that I didn't obverse the rules before answer (cuz it's my first answer). 对不起,我在回答之前没有遵守规则(因为这是我的第一个答案)。

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

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