How to stop TextView expanding too far past other items RelativeLayout?
I have a TextView and an ImageView in Relative Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingLeft="10dip" android:id="@+id/itemRoot"
android:clickable="false">
<TextView android:layout_width="wrap_content" android:text="TextView"
android:layout_height="wrap_content" android:id="@+id/itemTxt"
android:layout_alignParentLeft="true" android:layout_centerVertical="true"
android:singleLine="true"></TextView>
<ImageView android:id="@+id/delBtn" android:layout_height="wrap_content"
android:src="@drawable/delete" android:layout_width="wrap_content"
android:layout_alignParentRight="true" android:paddingRight="10dip"
android:layout_centerVertical="true"></ImageView>
</RelativeLayout>
However, when text gets too long it overlaps with the ImageView goes under it all the way to the end of the screen and then "...". But I want it to stop where ImageView begins. I can't stretch the TextView all the way to ImageView beca开发者_如何转开发use I also have some backgrounds underneath that look ugly. So I tried adding a blank, transparent spacer between the 2 Views:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/spacer"
android:layout_alignTop="@+id/itemTxt" android:layout_alignBottom="@+id/itemTxt"
android:layout_toLeftOf="@+id/delBtn" android:layout_toRightOf="@+id/itemTxt"
android:minWidth="5dip" android:background="@android:color/transparent"></TextView>
However, I still have the same problem. The spacer works nicely as long as text is not too long. But because itemTxt defined with respect to the ParentLeft it just pushes the spacer out of the screen, and I can't define it also with respect to the spacer as SDK complains that it is circular.
Any thoughts?
Alex
Is this what your looking for?
Keeps the textview above the Imageview:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="BLaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah"/>
Or do you mean next to each other?
Use a Linear Layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/itemRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dip"
android:orientation="horizontal"
android:clickable="false">
<TextView
android:id="@+id/itemTxt"
android:layout_width="0dip"
android:layout_weight="1"
android:text="TextView"
android:layout_height="wrap_content"
android:singleLine="true"/>
<ImageView
android:id="@+id/delBtn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingRight="10dip"
android:src="@drawable/delete"/>
</LinearLayout>
declare ImageView
before TextView
so that its id is already available and add the following attribute to the <TextView>
element: android:layout_toLeftOf="@id/delBtn"
First of all, it's not a good idea to use LinearLayout and weights instead of RelativeLayout, especially in case of using this LinearLayout inside of the ListView. And the reason for this is that Android makes two passes to render your LinearLayout when you're using weights. And that fact could spoil the performance of your ListView. Instead of using weights you have to declare your ImageView before the TextView in your layout xml, and then add this attribute to your TextView:
android:layout_toLeftOf="@id/delBtn"
in relative layout such problem can be solved by fixing the end point(right,left,top,right) using corresponding attributes
since we require to restraint width only so putting end points as per left and right will be enough
so only two attributes can do job
android:layout_toRightOf
.
android:layout_toLeftOf
so you if you want textView with id= place in between two View(TextView,ImageView,or any other) id=intensity (on left) and id=time(on right) following will be the code :
`<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/place"
android:layout_toRightOf="@id/intensity"
android:layout_toLeftOf="@id/time"
android:text="hokage's house konoha hidden leaf village naruto universe "
android:textStyle="bold"
android:layout_below="@id/proximity"
/>`
the above code gives us: image with both atributes of width and large text
note if only android:layout_toLeftOf
attribute is used then on smaller texts text may get alligned to the right side image with only one attribute and small text "hokage's home "
so using both attribute is better which on using with small text also it gives perfect result
精彩评论