ImageView 和 TextView 浮动(左/右)Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1939671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:51:38  来源:igfitidea点击:

ImageView and TextView float (left/right) Android

androidimagetextalignment

提问by wouter88

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

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

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

我怎么能在Android中做到这一点?我已经使用过android:layout_alignParentLeft="true"(将我的 imageview 放置在左侧)和android:layout_alignParentRight="true"我的 textview,但是 textview 位于 imageView 旁边并且当我有很长的文本时不会在 ImageView 下方继续..

Any ideas?

有任何想法吗?

回答by ScratchMyTail

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.

我在尝试将 ImageView 与 RelativeLayout 的左侧对齐时遇到了同样的问题。我使用 android:scaleType="fitStart" 来修复它。

My XML-file:

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

回答by Shubhayu

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.

没有简单的方法来完成这项工作。您可以使用 2 个文本视图,一个在图像视图旁边,一个在图像视图下方。如果你幸运地得到一个恒定大小的图像,你需要在它周围环绕文本(我是),计算适合图像旁边的文本视图的字符数并将你的字符串分成两部分。只要确保不要在单词中间拆分文本即可。

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.

我也尝试过使用 html,但为此我不得不使用 webview,它又重又脏。首选拆分文本版本。

回答by hwii77

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.

我也尝试了很多方法,但没有运气。我认为 Text 不会像在 html 中那样在当前的 Android 中流动。

回答by Jawad Zeb

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, ...
  1. 使用 LinearLayout,将方向设置为水平方向,以便图像先出现,然后是其余部分。
  2. 使用RelativeLayout,您可以使用样式android:layout_toLeftOft、android:layout_toRightOf、android:layout_below 或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.

但是,对于某些操作,它不如 CSS 灵活,例如,在图像周围环绕文本并不容易实现。

回答by Vinay

You need to use RelativeLayout as the container

您需要使用 RelativeLayout 作为容器