5

Tôi có relativelayout và trong đó có hai bản xem trước. Textview 1 được căn chỉnh trên cùng và bên trái với cha mẹ. Textview 2 được căn chỉnh ở bên phải của chế độ xem văn bản 1 và ở bên phải với bố cục. Điều này tạo ra một vấn đề nếu văn bản trong 1 quá dài, có nghĩa là 1 sẽ đẩy 2 ra khỏi trường hợp xấu nhất của màn hình.TextView và Ellipsize khi được ép bởi TextView khác

Làm cách nào để tôi có được hai điều này để "hy sinh" từ văn bản nr 1 thay vì 2? chiều rộng tối thiểu dường như không hoạt động. Mục tiêu cuối cùng của tôi là để ellipsize văn bản 1, nhưng dường như bản lề trên textview có kích thước chính xác ở nơi đầu tiên.

Đây là 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_alignParentLeft="true" 
      android:textSize="22sp" 
      android:layout_width="wrap_content" 
      android:lines="1" 
      android:text="sd sd asd sad asd asd saasd a"> 
</TextView> 

</RelativeLayout> 
+3

Thực ra, điều này hoàn toàn được trả lời bởi câu hỏi này: http://stackoverflow.com/questions/3785221/two-textviews-side-by-side-only-one-to-ellipsize – sandos

Trả lời

0

Bạn có thể dễ dàng làm điều này với một LinearLayout ngang sử dụng thuộc tính cân. Mã sau đây sẽ làm việc cho bạn. Tôi thiết lập id cho quan điểm của bạn cũng như vậy chỉ cần sao chép trực tiếp dán này và nó sẽ làm việc trong mã của bạn.

<?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> 
+1

Tôi gặp vấn đề với giải pháp này trong dự án của tôi, văn bản đầu tiên được bỏ qua như nó cần, nhưng để lại một khoảng trống ở cuối, bởi vì nó rút ngắn toàn bộ từ (và từ cuối cùng là "dài") để văn bản thứ hai xa hơn . Bất kỳ giải pháp nào cho điều này – Squeazer

0

get ý tưởng từ TextView của tôi và chỉ sử dụng TextView của mình như thế này, hy vọng nó làm việc:

    <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" /> 
0

Sử dụng thuộc tính android:shrinkColumns="0" của TableLayout.