[英]Putting ImageView close to the TextView
My problem is that when I try always put ImageView in the same layout as the TextView and when SetText(); 我的问题是,当我尝试将ImageView放在与TextView相同的布局中时,以及SetText(); writes text to the textview, ImageView goes outside the layout.
将文本写入textview,ImageView不在布局中。 Yeah, I know I could put the Image to the background, but my Image is set by the setImageResource and I cannot change it or the project goes down...
是的,我知道我可以把图像放到后台,但是我的图像是由setImageResource设置的,我不能改变它或者项目失效......
So here is my XML: 所以这是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="onhar.personalhealth.Main"
android:baselineAligned="false"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginBottom="100dp"
android:id="@+id/tip"
android:text=""
android:layout_gravity="bottom" />
<ImageView
android:layout_width="260dp"
android:layout_height="260dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:id="@+id/imageView2" />
</LinearLayout>
Thanks alot for someone who answers this! 非常感谢有人回答这个问题!
Use layout_weight attribute: 使用layout_weight属性:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginBottom="100dp"
android:id="@+id/tip"
android:text=""
android:layout_gravity="bottom" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:id="@+id/imageView2" />
Use android:layout_weight 使用android:layout_weight
Try this 尝试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="onhar.personalhealth.Main"
android:baselineAligned="false"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginBottom="100dp"
android:id="@+id/tip"
android:text=""
android:layout_gravity="bottom" />
<ImageView
android:layout_width="260dp"
android:layout_height="260dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:id="@+id/imageView2" />
</LinearLayout>
add this line into your textview. 将此行添加到textview中。 That will add image left side of your
TextView
android:drawableLeft="@drawable/YOUR_IMAGE"
这将添加
TextView
android:drawableLeft="@drawable/YOUR_IMAGE"
左侧的图像android:drawableLeft="@drawable/YOUR_IMAGE"
Here is full xml: 这是完整的xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="@drawable/YOUR_IMAGE"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="24dip"
android:maxLines="1"
android:ellipsize="end"/>
It seems you're using fixed widths for both views. 看来你正在为两个视图使用固定宽度。 So, you can use
FrameLayout
to keep them close together. 因此,您可以使用
FrameLayout
将它们保持在一起。 In this way whenever the TextView
overflows it will go beneath the ImageView
. 这样,只要
TextView
溢出,它就会出现在ImageView
下面。
<FrameLayout
android:layout_width="560dp"
android:layout_height="...">
<TextView
android:layout_width="260dp"
android:layout_height="..."
android:layout_gravity="left"/>
<ImageView
android:layout_width="300dp"
android:layout_height="..."
android:layout_gravity="right"/>
</FrameLayout>
Another option is using dynamic width for the ImageView
. 另一种选择是使用
ImageView
动态宽度。 In this way the ImageView
will be resized based on remaining space in that LinearLayout
. 这样,
ImageView
将根据LinearLayout
剩余空间调整大小。
For responsive UI you must use relative layout, it will scale and look the same in all device this is how you could achieve this. 对于响应式UI,您必须使用相对布局,它将在所有设备中进行缩放和外观相同,这是您实现此目的的方法。
<TextView
android:id="@+id/tip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/imageView2"
android:text="some \n really \n realllllyyyyy \n \n long \n text a \n who \n just \n do not \n\n\n\ fit "
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="260dp"
android:layout_height="260dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
Result 结果
Irrespective of length of text your image will always going to be on right side with size 260dp & your text will scale depending on width of device screen and nothing will go out of screen. 无论文本长度如何,您的图像将始终位于右侧,大小为260dp,您的文本将根据设备屏幕的宽度进行缩放,并且屏幕上不会有任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.