0

Как остановить checkbox от исчезновения в коде ниже, когда текст в TextView слишком длинный? Я не заинтересован в hardcoding max_width для TextView, и я хочу отобразить весь текст.Слишком длинный TextView делает другие виды исчезающими

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

      <TextView 
       android:id="@+id/tv" 
       style="@style/Text" 
       android:layout_weight="0" 
       android:text="@string/multiple_sounds" /> 

      <CheckBox 
       android:id="@+id/cb" 
       style="@style/Text" 
       android:layout_gravity="right" 
       android:layout_weight="1"/> 

     </LinearLayout> 

styles.xml

<style name="Text"> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:padding">@dimen/margin</item> 
    <item name="android:textSize">@dimen/text</item> 
    <item name="android:textAlignment">center</item> 
</style> 

ответ

2

Я хотел бы использовать вес. Добавьте android:weightSum в ваш LinearLayout со значением 1.
Для каждого элемента вашего LinearLayout добавьте вес. Например, 0.8 для текста и 0.2 для Checkbox.

Затем установите ширину 0dp для каждого элемента!

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

     <TextView 
      android:id="@+id/tv" 
      style="@style/Text" 
      android:layout_weight="0.8" 
      android:text="@string/multiple_sounds" /> 

     <CheckBox 
      android:id="@+id/cb" 
      style="@style/Text" 
      android:layout_gravity="right" 
      android:layout_weight="0.2"/> 

    </LinearLayout> 

и обновить свой стиль:

<style name="Text"> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:layout_width">0dp</item> 
     <item name="android:padding">@dimen/margin</item> 
     <item name="android:textSize">@dimen/text</item> 
     <item name="android:textAlignment">center</item> 
    </style> 
+0

+1 Вы читаете мой разум, пока я пишу ответ :). Дело в том, что ' 0dp' thats проблема и, конечно, нет оснований для использования 'android: layout_weight =" 0 "' on TextView –

+0

Спасибо! Он работает так, как ожидалось. – s1vert

-1

Если вы хотите текст идти в новую строку (использовать RelativeLayout):

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

<CheckBox 
    android:id="@+id/checkBox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" /> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toStartOf="@id/checkbox" /> 

</RelativeLayout> 

Другим решением является использование этой библиотеки, чтобы уменьшить текст : https://github.com/grantland/android-autofittextview

-1

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

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

<TextView 
    android:id="@+id/tv" 
    style="@style/Text" 
    android:layout_toLeftOf="@+id/cb" 
    android:text="@string/multiple_sounds" /> 

<CheckBox 
    android:id="@+id/cb" 
    style="@style/Text" 
    android:layout_gravity="right" 
    android:layout_alignParentEnd="true"/> 

</RelativeLayout> 
0

Если вы собираетесь показывать большие объемы данных на TextView, я предлагаю использовать вид прокрутки в вашем родительском макете.

Смежные вопросы