开发者

TextView and Ellipsize when squeezed by other TextView

I have a relativelayout, and in it two textviews. Textview 1 is aligned top and left with parent. Textview 2 is aligned to the right of textview 1, and to the right with parent. This creates a problem if the text in 1 is too long, meaning that 1 will push 2 out of the screen worst case.

How do I get these two to "sacrifice" from text nr 1 instead of 2? min width does not seem to work. My final goal is to ellipsize text 1, but seems to hinge on the textview to be sized correctly in the first place.

Here is the xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/TextView02"
          android:layout_alignParentRight="true"
          android:layout_toRightOf="@+id/TextView01"
          android:text="I am number 2"
          android:gravity="right"
          android:lines="1"
          android:minWidth="32sp">
</TextView>
<TextView android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:id="@+id/TextView01"
          android:layout_alignParent开发者_如何学CLeft="true"
          android:textSize="22sp"
          android:layout_width="wrap_content"
          android:lines="1"
          android:text="sd sd asd sad asd asd saasd a">
 </TextView>

</RelativeLayout>


You can easily do this with a horizontal linearlayout using weight attribute. The following code will work for you. I set id s for your views as well so just directly copy paste this and it will work in your code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/TextView01"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="Some Long and Random Text Some Long and Random Text"
        />
    <TextView
        android:id="@+id/TextView02"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="0"
        android:layout_gravity="right"    
        android:text="I am number 2"
     />
</LinearLayout>


get idea from my textview and just use your textview like this,hope it's work:

                 <TextView
                    android:id="@+id/tv_songtitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:text="Song Title"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:textSize="13dp"
                    android:textStyle="bold" />


Use attribute android:shrinkColumns="0" of TableLayout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜