2015-02-14 3 views
0

Я искал, но не мог решить проблему.Невозможно отрегулировать вес в линейном макете - андроид

enter image description here

Я хочу сделать так же, как на картинке выше. Я использовал линейную компоновку в вертикальном положении для «Статуса оценки дневных команд» А затем для содержимого, содержащего значения вышеуказанных заголовков, я использовал listview.

То, что я достиг, состоит в том, что заголовки «Статусы состояния дневных команд» отображаются с пробелами, но содержимое (которые находятся в отдельном .xml-файле) не отображаются с пробелами, поэтому я получаю:

enter image description here

XML-файл адаптер для просмотра списка является:

adapter_my_matches.xml

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


    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Date" 
     android:id="@+id/textViewDate" 
     android:layout_weight="0.1" 
     android:layout_gravity="center_vertical" /> 

    <LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_weight="0.4"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Teams" 
      android:id="@+id/textViewTeams1"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Teams" 
      android:id="@+id/textViewTeams2"/> 
    </LinearLayout> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Score" 
     android:id="@+id/textViewScore" 
     android:layout_weight="0.1" 
     android:layout_gravity="center_vertical" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Status" 
     android:id="@+id/textViewStatus" 
     android:layout_weight="0.2" 
     android:layout_gravity="center_vertical" /> 
</LinearLayout> 
+0

он разработал –

+0

да это работает код, но дает неожиданный макет. –

+0

@MuhammadRehanQadri, Пожалуйста, проверьте мой ответ и дайте мне знать –

ответ

1

Я думаю, что это макет вы ищете for.Let мне знать, если что-то будет плохо. Здесь вес i, установленный для образца, вы можете изменить его в соответствии с вашими потребностями.

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

    > 
    <LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum= "6"> 
     <TextView 
     android:layout_width="0dp" 

     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Date" 
     android:id="@+id/textViewDate" 
     android:layout_weight="1.2" 
     android:gravity="center" /> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="Teams" 
      android:id="@+id/textViewTeams1" 
      android:gravity="center" 
      android:layout_weight="2"/> 


     <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Score" 
     android:id="@+id/textViewScore" 
     android:layout_weight="1.4" 
     android:gravity="center" 
     /> 

     <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Status" 
     android:id="@+id/textViewStatus" 
     android:layout_weight="1.4" 
     android:gravity="center" 
     /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/ll1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum= "6"> 
     <TextView 
     android:layout_width="0dp" 

     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="12 Feb" 
     android:id="@+id/textViewDate1" 
     android:layout_weight="1.2" 
     android:gravity="center" /> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="IND VS PAK" 
      android:id="@+id/textViewTeams11" 
      android:gravity="center" 
      android:layout_weight="2"/> 


     <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="256" 
     android:id="@+id/textViewScore1" 
     android:layout_weight="1.4" 
     android:gravity="center" 
     /> 

     <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="WIN" 
     android:id="@+id/textViewStatus1" 
     android:layout_weight="1.4" 
     android:gravity="center" 
     /> 
    </LinearLayout> 

</LinearLayout> 
+0

Спасибо, что это сработало! Не могли бы вы упомянуть проблему с моим кодом? –

+0

Я проверил проблему bro, для меня, если текст большой, ее отображение в следующей строке :(. –

+0

Хорошо спасибо, Не могли бы вы рассказать мне, что не так с моим кодом? –

0

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

+0

, чью ширину нужно установить для упаковки содержимого. Последнее предложение вашего ответа расплывчато. Просьба уточнить. –

0

Используйте этот один:

<?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:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="8" > 

    <TextView 
     android:id="@+id/textViewDate" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1" 
     android:text="Date" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="4" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textViewTeams1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Teams" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/textViewTeams2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Teams" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/textViewScore" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="1" 
     android:text="Score" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textViewStatus" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="2" 
     android:text="Status" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 
+0

такой же проблема. У вас будет сцена вашего кода, работающая на вашем андроид-студийном предварительном просмотре xml. Но когда вы запускаете эмулятор или реальное устройство, оно не отображается так, как ожидалось. Получение такой же проблемы. –

+0

Вы хотите разницу между командой и счетом? –

+0

точно. Фактически разрыв между каждым столбцом. –

1

Добавьте это в ваш код LinearLayout

android:weightSum="0.8" 
Смежные вопросы