简体   繁体   English

如何设计带有textview和Image的布局,其中无论textview的长度如何,Image始终应在textview的右边对齐

[英]How to design a layout with textview and Image where Image always should align right of textview regardless of length of textview

I need to design a layout where my textview should be left and image on the right,if my text length increases and goes to second line then my image should be second line right. 我需要设计一个布局,其中我的textview应该在左边,图像在右边,如果我的文本长度增加到第二行,那么我的图像应该在第二行。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dhhad asuhdasdh saxax xsaxsx sdjsad sbadsa dcnc" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textview"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

样本图片]![在此处输入图片说明

在此处输入图片说明

Here is the solution... 这是解决方案...

Set the image to layout_alignParentRight="true" and then the textview layout_toLeftOf="@id/image" and this should solve your issue. 将图像设置为layout_alignParentRight =“ true” ,然后将文本视图设置为layout_toLeftOf =“ @ id / image” ,这应该可以解决您的问题。

Edit: You will also need to add android:layout_alignParentLeft="true" on the textView to make sure it stays left aligned when the text is shorter. 编辑:您还需要在textView上添加android:layout_alignParentLeft =“ true” ,以确保当文本较短时它保持左对齐。

Obviously I have just mocked it up using an empty imageview with a background colour to test, you will need to make this fit into your example. 显然,我只是使用带有背景色的空imageview进行了测试,您需要使其适合您的示例。

<RelativeLayout
    android:id="@+id/parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#000"
        android:layout_alignParentRight="true"/>

    <TextView
        android:id="@+id/textView"
        android:text="Some text here to see what happens. does it wrap?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/image"
        />
</RelativeLayout>

try this 尝试这个

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:drawablePadding="10dp"
    android:drawableRight="@drawable/ic_launcher"
    android:text="dhhad asuhdasdh saxax xsaxsx sdjsad sbadsa dcnc" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM