Android TextView和ImageView的对齐
我试图在一行中设置文本和图像(小图像)。要求是文本应该左对齐,图像应该右对齐。如果文本较大,则不应与图像重叠,而是应分为两行或更多行。 我正在尝试下面的代码,但它不起作用。首先,图像未右对齐,如果文本较大,则图像根本不出现,只有文本出现在多行中:Android TextView和ImageView的对齐,android,android-linearlayout,android-imageview,textview,Android,Android Linearlayout,Android Imageview,Textview,我试图在一行中设置文本和图像(小图像)。要求是文本应该左对齐,图像应该右对齐。如果文本较大,则不应与图像重叠,而是应分为两行或更多行。 我正在尝试下面的代码,但它不起作用。首先,图像未右对齐,如果文本较大,则图像根本不出现,只有文本出现在多行中: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_c
<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>
请尝试以下代码-
@Sid如果有问题或您有任何问题,请与我联系。谢谢Nikhil,现在文本左对齐,图像右对齐(根据要求),但现在图像的高度和宽度不是固定的(我在这里明确设置),它是根据文本后可用的空间设置宽度和高度。如果可以,请帮助。@Sid我不明白,请解释你的问题。你想把图像的高度和宽度调整到正确的位置。就像如果文本视图中有小文本(比如Abcd),那么图像的宽度就会变大,如果文本视图中有大文本(比如这是VeryVeryVeryVeryVeryUyVeryVeryVeryVeryTargetText),那么图像的宽度就会变小。这就是图像的宽度不是固定的。看看我在上面给出的代码安卓:layout_width=“@dimen/congreats_img_width”@Sid如果你想这样做,你可以使用图像的layout:height和layout:width属性,并以xxdp的形式给出它。
<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>