3

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

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingEnd="12dp" 
    android:paddingStart="8dp" 
    android:paddingTop="4dp" 
    android:paddingBottom="4dp"> 
    <LinearLayout 
     android:orientation="horizontal"  
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="6" 
     android:gravity="center" > 
     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="4dp"/> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="4"> 
      <TextView 
       android:id="[email protected]/receipt_item_name" 
       android:layout_width="wrap_content" 
       android:textAlignment="viewStart" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="item name" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:textAlignment="viewStart" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/receipt_item_name" 
       android:text="selected by 2 others" 
       android:textSize="10sp"/> 
     </LinearLayout> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="1" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:textAlignment="textEnd" 
      android:text="10$" /> 
    </LinearLayout> 
</RelativeLayout> 

Но когда текст в receipt_item_name слишком долго это делает следующий
TextView сдвиг вправо. Как я могу убедиться, что вес каждого представления прочен?
или Что еще я могу сделать, чтобы получить тот же эффект?

looks terrible..

ответ

2

Изменение width каждого макета, который имеет те weight до 0, как это:

<LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="4"> 

И для этих двух, а также:

<TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="1" /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textAlignment="textEnd" 
     android:text="10$" /> 

Дело в том, когда вы устанавливаете как weight, так и width, width имеет преимущество и ign ores атрибут weight.

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