0

Я пытаюсь создать отзывчивый макет с использованием весов, но в этом случае мне также нужно использовать прокрутку.Linear Layout with Weights and Scroll View

Это мой код в данный момент:

<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" 
tools:context=".EncyclopediaFragment"> 

    <!--Linear Container--> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="100"> 

     <!--Title Box--> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="20" 
      android:orientation="horizontal" 
      android:weightSum="100"> 

      <!--Empty Space--> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="5"> 


      </LinearLayout> 

      <!--Text Box--> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="95"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="@string/encyclopedia_mosntersLabel" 
        android:gravity="center" 
        android:textAlignment="viewStart"/> 

      </LinearLayout> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="40"> 

     </LinearLayout> 

    </LinearLayout> 

, но она должна быть как this.

Этот макет, как предполагается, должен реагировать с весами, но в то же время мне нужно прокручивать.

Итак, мой вопрос: как я могу создать макет с учетом веса и в то же время макет, который может прокручиваться вниз, как на картинке?

+0

Сколько монстра и сколько башни? Я имею в виду группу? Это одна группа монстров и одна группа Башен [как из изображения]? – OBX

+0

вы можете просто разместить макет внутри прокрутки. Ранее я использовал как макет сетки, так и линейную компоновку с весами для той же цели. – ArchiFloyd

+0

Вот частичный совет. Для ваших монстров, пересекающихся, я предлагаю строку таблицы (width = match parent, gravity = center), каждый монстр (width = 0dp, weight = 1) ..... не используйте весовую сумму. – seekingStillness

ответ

1

Если у вас есть определенное количество 6 монстров и 8 башен, вот как вы должны организовать макет:

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

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

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Monsters"/> 

      <!-- the following LinearLayout should be repeated twice for 6 Monsters --> 

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

       <MonsterView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

       <MonsterView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

       <MonsterView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

      ... 


      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Towers"/>   

      <!-- the following LinearLayout should be repeated twice for 6 Towers --> 

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

       <TowerView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

       <TowerView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

       <TowerView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

      ... 


      <!-- attention here for two Towers in the last row -->   

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

       <TowerView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

       <TowerView 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" /> 

      </LinearLayout> 

     </LinearLayout> 
    </ScrollView> 
+0

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

+0

Вы можете определить ресурс 'dimension' в значениях или размер файла:' 20dp 'и ссылаться на него внутри тега MonsterView:' android: layout_height = "@ dimen/monster_height" ' – foxanna

+0

So , нет смысла использовать вес? –

0
<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" 
tools:context=".EncyclopediaFragment"> 

<!--Toolbar--> 
<ScrollView 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

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

<!--Linear Container With Weights--> 

</RelativeLayout> 
</ScrollLayout> 
</RelativeLayout> 
+0

Это бесполезно. Внешний 'RelativeLayout' ничего не делает. И макет внутри 'ScrollView' должен иметь' android: layout_height = "wrap_content" '. Иначе это не имеет смысла! – foxanna

0

Содержание внутри вашего ScrollView должны использовать wrap_parent, и если вы хотите, чтобы растянуть содержимое внутри него установлен android:fillViewport="true". Попробуйте это:

<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" 
tools:context=".EncyclopediaFragment"> 

    <ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

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

      <!--Linear Container--> 

     </RelativeLayout> 
    </ScrollLayout> 
</RelativeLayout>