2016-11-25 1 views
-1

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

Bottom Bar Вид

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bottom_bar_root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/bottom_bar_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="4" 
     android:orientation="horizontal"/> 
</RelativeLayout> 

Это большой контейнер я добавить мои кнопки (пункты) в.

Bottom Bar Item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 
    <android.support.v7.widget.AppCompatImageView 
     android:id="@+id/bottom_bar_item_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/arrow_up_float"/> 
    <TextView 
     android:id="@+id/bottom_bar_item_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TEXT"/> 
</LinearLayout> 

Это мои вещи, которые я добавить динамически представление. Но почему-то вид не растягивается должным образом.

Но когда я его закодировал, он работает. Так может ли быть, что вес макета не работает динамически?

Как я добавить просмотр (элементы)

private void updateItems(final List<BottomBarTab> bottomBarTabs) { 
    if (bottomBarTabs.size() < TABS_COUNT) { 
     throw new RuntimeException("Not enough buttons for bottomBar"); 
    } 

    for (int i = 0; i < TABS_COUNT; i++) { 
     bottomBarTabs.get(i).setOnClickListener(this); 

     bottomBarTabs.get(i).prepareLayout(); 
     container.addView(bottomBarTabs.get(i)); 
    } 
} 

Скриншот

enter image description here

+0

показать полную схему размещения. Лучше всего было бы с скриншотом –

+0

Готово. Добавлено полное представление xml. + что с нисходящим. Это законная проблема. Потому что я занимался исследованиями самостоятельно и заметил, что динамически я не могу заставить его работать. Но hardcoding вид с его детьми работал –

+0

@Jemil Riahi вы можете опубликовать код 'container' view и' prepareLayout() 'метод? –

ответ

0
public void prepareLayout() { 
    View view = inflate(getContext(), R.layout.bottom_bar_item,this); 

    LinearLayout.LayoutParams params = 
      new LayoutParams(0,LayoutParams.MATCH_PARENT,LAYOUT_WEIGHT); 

    view.setLayoutParams(params); 


    if(backgroundColor != null) { 
     view.setBackgroundColor(backgroundColor); 
    } 

    TextView titleText = (TextView) view.findViewById(R.id.bottom_bar_item_text); 
    titleText.setText(title); 

    AppCompatImageView imageView = (AppCompatImageView) view.findViewById(R.id.bottom_bar_item_icon); 
    imageView.setImageResource(iconResId); 
} 

Я изменил в моей функции prepareLayout и поставить новые layoutParams. Потому что как-то после инфляции. В представлении игнорируется установленный для него вес. Так что мне пришлось форсировать его по коду. Может быть, это ошибка Android?

0

LayoutWeight отдается внутренним компонентам макете только не на родительском LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:weightSum="2"> 
     <android.support.v7.widget.AppCompatImageView 
      android:id="@+id/bottom_bar_item_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
android:layout_weight="1" 
      android:src="@android:drawable/arrow_up_float"/> 
     <TextView 
      android:id="@+id/bottom_bar_item_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
android:layout_weight="1" 
      android:text="TEXT"/> 
    </LinearLayout> 
+0

да, но другой linearLayout, имеющий вес, представляет собой дочерний вид родительский контейнер –

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