对齐TextView和ImageView
问题描述:
我想在一行中设置文本和图像(小图像)。要求是文本应该左对齐,并且图像应该右对齐。如果文字很大,那么它不应该与图像重叠,而应该出现在两行或更多行中。 我正在尝试这与下面的一段代码,但它不工作。所有图像的第一个没有右对齐,如果文本大,图像不来可言,只有文本即将在多行:对齐TextView和ImageView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/answerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:focusable="true"
android:gravity="left"
android:textSize="@dimen/answer_size"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="3dip"
android:layout_height="wrap_content" >
</View>
<ImageView
android:id="@+id/congratsImageView"
android:layout_width="@dimen/congrats_img_width"
android:layout_height="@dimen/congrats_img_height"
android:layout_gravity="right"
android:adjustViewBounds="false"
android:scaleType="fitXY" >
</ImageView>
</LinearLayout>
我RelativeLayout的尝试也,在这种情况下,图像和文本都是左对齐,图像与左边的文字重叠。 请注意,这里还有LinearLayout上下的其他字段,我在运行时从Java方法中提取图像。 任何帮助将不胜感激。
答
使用android:weight属性赋予它们相等的空间。
看我修改了你的代码。它会100%的工作使用它。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/answerTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:focusable="true"
android:gravity="left"
android:text="Hello world.Hello world.Hello world.Hello world.Hello world.Hello world. "
android:textColor="@android:color/white"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="3dip"
android:layout_height="wrap_content" >
</View>
<ImageView
android:id="@+id/congratsImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:adjustViewBounds="false"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
</LinearLayout>
答
试试下面的代码 -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/answerTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="true"
android:gravity="left"
android:text="Hello world.Hello world.Hello world.Hello world.Hello world.Hello world. "
android:textColor="@android:color/white"
android:textStyle="bold" >
</TextView>
<View
android:layout_width="3dip"
android:layout_height="wrap_content" >
</View>
<ImageView
android:id="@+id/congratsImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" >
</ImageView>
</LinearLayout>
</LinearLayout>
@Sid刚刚回来给我,如果它给了问题,或者您有任何问题。 – 2013-04-22 19:23:25
感谢Nikhil,现在文本左对齐和图像右对齐(根据要求),但现在图像的高度和宽度不固定(我明确设置在这里),它基于文本后可用空间设置宽度和高度。如果可以,请帮助。 – Sid 2013-04-22 19:37:46
@Sid我不理解请解释你的问题。你想修正图像的高度和宽度。 – 2013-04-22 19:39:38