2017-01-23 3 views
0

Вот что я хочу получить в результате, и я получаю за API < = 17 и API> = 20: Correct result on API25Как сделать layout_weight одинаковым для API18-19, как и для других уровней API?

Однако для API 18-19 высоты FrameViews в не уважает layout_weight значения:

enter image description here

проблема может быть связана с тем, что, чтобы сделать panelLayout высота действительно match_parent, мне нужно добавить layout_alignParentTop="true" и layout_alignParentBottom="true".

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

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/wrappingScrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:fillViewport="true" 
    > 

    <RelativeLayout 
     android:id="@+id/content" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_green_light" 
     > 

     <View 
      android:id="@+id/longView" 
      android:layout_width="150dp" 
      android:layout_height="600dp" 
      android:layout_margin="10dp" 
      android:background="@android:color/holo_red_light" 
      /> 

     <LinearLayout 
      android:id="@+id/panelLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_alignBottom="@id/longView" 
      android:layout_alignLeft="@id/longView" 
      android:layout_alignTop="@id/longView" 
      android:layout_margin="10dp" 
      android:background="@android:color/holo_blue_bright" 
      android:orientation="vertical" 
      > 

      <FrameLayout 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_margin="10dp" 
       android:layout_weight="1" 
       android:background="@android:color/holo_purple" 
       android:padding="5dp" 
       > 

       <View 
        android:layout_width="48dp" 
        android:layout_height="48dp" 
        android:layout_gravity="center" 
        android:background="@android:color/black" 
        /> 
      </FrameLayout> 

      <FrameLayout 
       android:layout_width="wrap_content" 
       android:layout_height="0dp" 
       android:layout_margin="10dp" 
       android:layout_weight="1" 
       android:background="@android:color/holo_purple" 
       android:padding="5dp" 
       > 

       <View 
        android:layout_width="48dp" 
        android:layout_height="48dp" 
        android:layout_gravity="center" 
        android:background="@android:color/black" 
        /> 
      </FrameLayout> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 

Не у вас есть идея, как сделать Android выложит эти взгляды одинаково на всех уровнях API от 17 до? Рассмотрим размер динамика longView.

+1

Обновить RelativeLayout height to match_parent. – Rahul

+0

@PavneetSingh Я нашел что-то еще, глядя больше на прилагаемые скриншоты. Вид занимает полную высоту по размеру экрана. Проблемы, похоже, связаны с весовым свойством FrameLayout. http://stackoverflow.com/questions/26837978/frame-layout-not-adjusting-layout-weight – Rahul

+0

Вы можете присоединиться ко мне здесь - https://join.skype.com/CP1OA5QWz58c – Rahul

ответ

1

Изменить FrameLayoutRelativeLayout которые имеют фиолетовый цвет и свойство высоты корня RelativeLayout должно быть match_parent.

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/wrappingScrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    android:fillViewport="true"> 

<RelativeLayout 
    android:id="@+id/content" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_green_light"> 

    <View 
     android:id="@+id/longView" 
     android:layout_width="50dp" 
     android:layout_height="600dp"/> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="600dp" 
     android:layout_margin="10dp"> 

    <LinearLayout 
     android:id="@+id/panelLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" 
     android:background="@android:color/holo_blue_bright" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_margin="10dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_purple" 
      android:padding="5dp"> 

      <View 
       android:layout_width="48dp" 
       android:layout_height="48dp" 
       android:layout_centerInParent="true" 
       android:background="@android:color/black" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_margin="10dp" 
      android:layout_weight="1" 
      android:background="@android:color/holo_purple" 
      android:padding="5dp"> 

      <View 
       android:layout_width="48dp" 
       android:layout_height="48dp" 
       android:layout_centerInParent="true" 
       android:background="@android:color/black" /> 
     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout> 
</RelativeLayout> 

</ScrollView> 
+1

хороший ответ, +1 –

+0

К сожалению, это все тот же результат :-(Вы даже можете попробовать сами, просто добавьте Activity с этим макетом, и вы сами можете убедиться, что на API18-19 это не работает так, как ожидалось ... – ohin

0

В дополнение к принятому решению, вот как обойти макет ошибка программно:

 panelLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
      @Override 
      public void onLayoutChange(final View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
       if (bottom != oldBottom) { 
        v.post(new Runnable() { 
         @Override 
         public void run() { 
          v.getLayoutParams().height = mButtonsStripLeft.getHeight(); 
          v.requestLayout(); 
         } 
        }); 
       } 
      } 
     }); 

Это позволяет изменять высоту longView динамически и до сих пор обходной путь работать должным образом.

Я не смог заставить его работать, с animateLayoutChanges="true" установлен на panelLayout. Поэтому он должен быть отключен.

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