简体   繁体   中英

ImageView and TextView float (left/right) Android

I am searching for a way to position my text around my imageview (so like the css float left/right).

How can I do this in Android? I have used android:layout_alignParentLeft="true" (to position my imageview left) and android:layout_alignParentRight="true" for my textview but the textview comes next to the imageView and doesn't continue underneath the ImageView when I have a long text..

Any ideas?

I had the same problem trying to get a ImageView aligned to the left of a RelativeLayout. I used android:scaleType="fitStart" to get it fixed.

My XML-file:

 <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:padding="10dp">
     <ImageView
         android:layout_height="50dp"
         android:id="@+id/country_icon"
         android:layout_alignParentLeft="true"
         android:layout_width="wrap_content"
         android:scaleType="fitStart" />          
</RelativeLayout>

I've also tried many ways but no luck. I don't think Text would flow in the current Android as it does in html.

There is no easy way to get this done. You can either use 2 text views, one next to the imageview and one below the imageview. And if you are lucky in getting a constant sized image around which you need to wrap the text (i was), calculate the number of characters that would fit in the textview next to the image and break your string in two parts. Just make sure that you don't split the text in the middle of a word.

I had also tried using html, but for that i had to use a webview, it was heavy and dirty. Preferred the splitting text version.

您需要使用RelativeLayout作为容器

You have two options:

  1. With LinearLayout, setting the orientation to horizontal so the image is first and then come the rest.
  2. With RelativeLayout, there you can indicate the position of a element relative to another with the attributes of the style android:layout_toLeftOft, android:layout_toRightOf, android:layout_below or android:layout_alignParentTop, ...

However, it is not so flexible as CSS for some actions and, for example, wrapping text around an image is not so easy to achieve.

// suppose "textview_title_label" is the textview object
       
    
// Then make sure to set the textview to match_parent also make sure that the parent 
biger size in width so you will notice the float
        val params_textview_title_label: LinearLayout.LayoutParams =
            LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )

 // this will make it float left to the end right
        textview_title_label.textAlignment = View.TEXT_ALIGNMENT_TEXT_END

 // set the object of extra parameter include the match_parent
        textview_title_label.setLayoutParams(params_textview_title_label)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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