2016-03-04 3 views
1

Я пытаюсь создать сетку кнопок с сопроводительными заголовками. Когда я создаю одну кнопку с ее названием, проблем нет.Вертикальный LinearLayout в вертикальном LinearLayout игнорирует макет: вес

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="org.havenseedsforsuccess.havenseedsforsuccess.PlantList" 
    android:background="@drawable/gradientfill"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="What&apos;s Growing?" 
     android:id="@+id/textPlantListTitle" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textColor="@color/colorBrown" 
     android:textSize="30dp" 
     android:gravity="center" 
     android:layout_marginBottom="10dp" /> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/scrollView" 
     android:fillViewport="true" 
     android:layout_weight="9.25" 
     android:layout_below="@+id/textPlantListTitle" 
     android:layout_centerHorizontal="true"> 

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

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:weightSum="12" 
       android:layout_marginRight="5dp" > 

       <ImageButton 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:id="@+id/imageButton" 
        android:src="@drawable/plantfiller" 
        android:scaleType="fitCenter" 
        android:background="@android:color/transparent" 
        android:layout_weight="3.5" /> 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Plant Name" 
        android:id="@+id/textView4" 
        android:textColor="@color/colorBrown" 
        android:gravity="center" 
        android:textSize="20dp" 
        android:layout_weight=".5" /> 

      </LinearLayout> 

     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

Код выше дает мне раскладку я ищу:

enter image description here

Но, когда я скопировать и вставить вертикальную LinearLayout кнопку/TITLE, чтобы создать второй под ним, все становится все вялым и растянутым. Макет: вес больше не соблюдается, и каждая кнопка/название LinearLayout занимает примерно половину экрана.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="org.havenseedsforsuccess.havenseedsforsuccess.PlantList" 
    android:background="@drawable/gradientfill"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="What&apos;s Growing?" 
     android:id="@+id/textPlantListTitle" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textColor="@color/colorBrown" 
     android:textSize="30dp" 
     android:gravity="center" 
     android:layout_marginBottom="10dp" /> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/scrollView" 
     android:fillViewport="true" 
     android:layout_weight="9.25" 
     android:layout_below="@+id/textPlantListTitle" 
     android:layout_centerHorizontal="true"> 

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

      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:weightSum="12" 
       android:layout_marginRight="5dp" > 

       <ImageButton 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:id="@+id/imageButton" 
        android:src="@drawable/plantfiller" 
        android:scaleType="fitCenter" 
        android:background="@android:color/transparent" 
        android:layout_weight="3.5" /> 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Plant Name" 
        android:id="@+id/textView" 
        android:textColor="@color/colorBrown" 
        android:gravity="center" 
        android:textSize="20dp" 
        android:layout_weight=".5" /> 

       <ImageButton 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:id="@+id/imageButton2" 
        android:src="@drawable/plantfiller" 
        android:scaleType="fitCenter" 
        android:background="@android:color/transparent" 
        android:layout_weight="3.5" /> 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Plant Name" 
        android:id="@+id/textView2" 
        android:textColor="@color/colorBrown" 
        android:gravity="center" 
        android:textSize="20dp" 
        android:layout_weight=".5" /> 


      </LinearLayout> 

     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

Приведенный выше код дает мне это:

enter image description here

+0

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

+0

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

+0

Почему у вас есть 3 LinearLayouts внутри друг друга? –

ответ

0

Ваша проблема является <ScrollView>. Вы пытаетесь изменить размер кнопок с помощью тега <weight>, но <ScrollView> делает ваш экран бесконечным, поэтому андроид просто не изменяет размер.

Просто удалите <ScrollView>, и я думаю, что вы достигнете того, чего хотите.

Если вам понадобится <ScrollView>, вам нужно много кнопок в стиле Google Play, выполните некоторые исследования и проверьте размеры экрана и просто сделайте <weight> фиксированное значение для каждого размера экрана, так как вам не нужно беспокоиться, если все кнопки отображаются на экране.

+0

Итак, вместо использования весов я должен использовать высоту набора для каждого размера экрана? –

+0

Это зависит от того, хотите ли вы использовать ScrollView и по-прежнему заполнять экран с помощью, например, 4 кнопок. Если вы сделаете так много кнопок, как ваша первая кнопка, просто удалите scrollView. –

+0

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

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