2017-01-24 8 views
0

Я должен выровнять два TextViews на одной полосе. Но первый TextView должен быть обрезан, когда он слишком длинный.Как выровнять два TextViews в одной строке с левой стороны

<RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

       <TextView 
        android:id="@+id/subtitle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:maxLines="1" 
        tools:text="subtitle" /> 

       <TextView 
        android:id="@+id/date" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/subtitle" 
        android:maxLines="1" 
        tools:text=" * date" /> 
      </RelativeLayout> 

У меня проблема. Когда первый TextView имеет слишком длинный текст, значит, второй TextView скрыт.

First case. Short subtitle

Second case. Long subtitle. Second TextView is cropped

Обе точки зрения должны быть размещены на левой стороне, когда текст короток. Но первый текст следует обрезать, когда текст длинный.

+0

Я не могу использовать вес, потому что эта точка зрения используется в RecyclerView и производительность может уменьшиться. – MonStar

+0

Вес также не является хорошим решением, так как представление может иметь длинную ширину, но TextViews имеют разную ширину. – MonStar

ответ

0

Для достижения обоих, вам нужно wrap_content в макете wrap_content во втором TextView и сочетание ширины: 0dp и layout_weight: 1 в первом TextView. Таким образом, второй TextView всегда будет достаточно большим, чтобы отображать содержимое, и первый TextView будет расширяться до доступного места рядом с ним.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/subtitle" 
     android:layout_width="0dp" 
     android:layout_height="40dp" 
     android:layout_weight="1" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:text="this text is tool long so it will be cut. this text is tool long so it will be cut." /> 

    <TextView 
     android:id="@+id/date" 
     android:layout_width="wrap_content" 
     android:layout_height="40dp" 
     android:maxLines="1" 
     android:text=" * date" /> 
</LinearLayout> 

enter image description here

+0

Большое спасибо за помощь. Это лучшее решение в моей ситуации. – MonStar

0

Добавить отступы как в TextView

android:paddingLeft="10dp" 

  <TextView 
       android:id="@+id/subtitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:maxLines="1" 
       android:paddingLeft="10dp" 
       tools:text="subtitle" /> 

      <TextView 
       android:id="@+id/date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@+id/subtitle" 
       android:maxLines="1" 
       android:paddingLeft="10dp" 
       tools:text=" * date" /> 
     </RelativeLayout> 
0

ли это может помочь в равном размере. Я дал им макет компоновки, вы можете изменить его и соответствующим образом отрегулировать текст. Вы можете прочитать больше о линейной массе макета от here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     > 

     <TextView 
      android:id="@+id/subtitle" 
      style="@style/m4w_TextView.Subtitle" 
      fontPath="fonts/InterstatePro-Light.otf" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:ellipsize="end" 
      android:layout_weight="1" 
      tools:text="subtitle" /> 

     <TextView 
      android:id="@+id/date" 
      style="@style/m4w_TextView.Subtitle" 
      fontPath="fonts/InterstatePro-Light.otf" 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:ellipsize="end" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/subtitle" 
      android:maxLines="1" 
      tools:text=" * date" /> 
    </LinearLayout> 

</LinearLayout> 
0

Попробуйте код ниже. Вам просто нужно сначала указать элемент даты, а затем субтитр в относительной компоновке.

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

<TextView 
       android:id="@+id/date" 
       android:layout_width="30dp" 
       android:layout_height="wrap_content" 
       android:maxLines="1" 
       tools:text=" * date" /> 

      <TextView 
       android:id="@+id/subtitle" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:maxLines="1" 
       android:layout_toLeftOf="@+id/date" 
       tools:text="subtitle" /> 


     </RelativeLayout> 
0

Попробуйте этот код может помочь вам

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/subtitle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:maxLines="1" 
      android:layout_weight="1" 
      tools:text="ajay bhimani this is practice"/> 

     <TextView 
      android:id="@+id/date" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/subtitle" 
      android:maxLines="1" 
      android:layout_weight="1" 
      tools:text=" * date"/> 
    </LinearLayout> 
0
<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/subtitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight = 1 
       tools:text="subtitle" /> 

      <TextView 
       android:id="@+id/date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight = 1 
       tools:text=" * date" /> 
     </LinearLayout > 
1

Используйте макет с weight:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView 
    android:id="@+id/subtitle" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:maxLines="1" 
    android:text="subtitlevvnvnvnnvnvnvnvnvnvnvnvnvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" 
    android:ellipsize="end" 
    android:layout_weight="0.5" /> 

<TextView 
    android:id="@+id/date" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:maxLines="1" 
    android:text=" * date" 
    android:layout_weight="0.5" /> 
</LinearLayout> 

Выход:

enter image description here

+1

это не выравнивает два текстовых поля слева, когда текст в текстовом окне слева короткий –

+0

@ BjörnKechel, вам нужно предоставить определенную ширину для работы с 'ellipsize', что делает ее' wrap_content' будет соответствовать родительскому. – W4R10CK

0

Попробуйте ниже код

<LinearLayout 
    android:layout_width="match_parent" 
    android:weightSum="2" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/subtitle" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:layout_weight="1" 
     android:ellipsize="end" 
     tools:text="subtitle df ds fgfg cvb vbcfvnb cbxfcb" /> 

    <TextView 
     android:id="@+id/date" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:maxLines="1" 
     tools:text=" * date" /> 
</LinearLayout> 
0

Попробуйте заменить RelativeLayout на LinearLayout и добавить layout_weight=1 для обоих TextViews

Изменение To:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/subtitle" 
     style="@style/m4w_TextView.Subtitle" 
     fontPath="fonts/InterstatePro-Light.otf" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:maxLines="1" 
     tools:text="subtitle" /> 

    <TextView 
     android:id="@+id/date" 
     style="@style/m4w_TextView.Subtitle" 
     fontPath="fonts/InterstatePro-Light.otf" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_toRightOf="@+id/subtitle" 
     android:maxLines="1" 
     tools:text=" * date" /> 
</LinearLayout>