ImageView和TextView在Android中如何浮动(左/右)

19

我正在寻找一种在图像视图周围定位文本的方法(就像css中的左/右浮动)。

在Android中如何实现呢?我已经使用了android:layout_alignParentLeft="true"(将我的ImageView定位到左侧),以及 android:layout_alignParentRight="true"(定位我的TextView到右侧),但是当我的文本很长时,TextView会出现在ImageView的旁边而不是在下面。有什么想法吗?

是否有其他属性可以对TextView进行定位?

6个回答

6

我曾经也遇到过同样的问题,想要将一个 ImageView 对齐到一个 RelativeLayout 左侧。我使用了 android:scaleType="fitStart" 修复了这个问题。

我的 XML 文件如下:

<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>

2

我也尝试了很多方法,但都没有成功。我认为在当前的安卓系统中,文本不会像在HTML中那样流畅。


我能否使用带有HTML的样式文本和float:left的<img>标签呢? - wouter88

1

没有简单的方法可以完成这个任务。您可以使用两个文本视图,一个在图像视图旁边,另一个在图像视图下方。如果您有幸获得一个固定大小的图像,需要将文本包装在其周围(我就是这样),则计算适合放置在图像旁边的文本视图中的字符数,并将字符串分成两部分。只需确保不要在单词中间拆分文本。

我也尝试过使用HTML,但为此我必须使用Webview,它很重且不太好用。更喜欢分割文本版本。


0
// 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)

0
你需要使用RelativeLayout作为容器。

是的,我使用RelativeLayout作为周围的布局...但它不起作用:s - wouter88

0

你有两个选择:

  1. 使用LinearLayout,将方向设置为水平,以便首先显示图像,然后显示其余内容。
  2. 使用RelativeLayout,您可以使用样式属性android:layout_toLeftOft、android:layout_toRightOf、android:layout_below或android:layout_alignParentTop来指定一个元素相对于另一个元素的位置...

然而,它并不像CSS那样灵活,对于某些操作比如环绕图像的文字并不容易实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接