Android Text over image

前端 未结 9 994
既然无缘
既然无缘 2020-12-02 06:44

I have an imageView with an image, over that image I want to place a text. How can I achieve that?

相关标签:
9条回答
  • 2020-12-02 06:54

    You could possibly

    • make a new class inherited from the Class ImageView and
    • override the Method onDraw. Call super.onDraw() in that method first and
    • then draw some Text you want to display.

    if you do it that way, you can use this as a single Layout Component which makes it easier to layout together with other components.

    0 讨论(0)
  • 2020-12-02 06:55

    You can use a TextView and change its background to the image you want to use

    0 讨论(0)
  • 2020-12-02 06:59

    You may want to take if from a diffrent side: It seems easier to have a TextView with a drawable on the background:

     <TextView
                android:id="@+id/text"
                android:background="@drawable/rounded_rectangle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
            </TextView>
    
    0 讨论(0)
  • 2020-12-02 06:59

    You want to use a FrameLayout or a Merge layout to achieve this. Android dev guide has a great example of this here: Android Layout Tricks #3: Optimize by merging.

    0 讨论(0)
  • 2020-12-02 07:04

    Try the below code this will help you`

      <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/gallery1"/>
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="#7ad7d7d7"
            android:gravity="center"
            android:text="Juneja Art Gallery"
            android:textColor="#000000"
            android:textSize="15sp"/>
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-02 07:05

    For this you can use only one TextView with android:drawableLeft/Right/Top/Bottom to position a Image to the TextView. Furthermore you can use some padding between the TextView and the drawable with android:drawablePadding=""

    Use it like this:

    <TextView
        android:id="@+id/textAndImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    
        android:drawableBottom="@drawable/yourDrawable"
        android:drawablePadding="10dp" 
        android:text="Look at the drawable below"/>
    

    With this you don't need an extra ImageView. It's also possible to use two drawables on more than one side of the TextView.

    The only problem you will face by using this, is that the drawable can't be scaled the way of an ImageView.

    0 讨论(0)
提交回复
热议问题