2016-11-25 4 views
-1

Ich habe ein kleines Problem, meine layout_weight zu arbeiten. Ich erstelle eine benutzerdefinierte BottomBar. Aber ich kann es nicht so strecken, wie ich es möchte.Layout-Gewicht funktioniert nicht wie vorgesehen

Bottom Bar View

<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> 

Dies ist der große Container i meine Tasten am Hinzufügen (Artikel) zu.

Bottom Bar Artikel

<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> 

Dies ist meine Einzelteile, die ich dynamisch zu der Ansicht hinzuzufügen. Aber irgendwie ist die Ansicht nicht richtig gestreckt.

Aber wenn ich es fest einprogrammiere. Es funktioniert. Könnte es sein, dass das Layout-Gewicht nicht dynamisch funktioniert?

Wie ich hinzufügen, die Ansichten (Artikel)

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)); 
    } 
} 

Screenshot

enter image description here

+0

zeigen Sie das komplette Layout pls. Beste Sache wäre mit einem Screenshot –

+0

fertig. Die vollständige XML-Ansicht wurde hinzugefügt. + was ist mit dem Downvote. Es ist ein echtes Problem. Weil ich selbst recherchiert habe und festgestellt habe, dass ich nicht in der Lage bin, es zum Laufen zu bringen. Aber hardcoding die Ansicht mit seinen Kindern arbeitete –

+0

@Jemil Riahi können Sie den Code der 'Container'-Ansicht und' prepareLayout() 'Methode? –

Antwort

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); 
} 

änderte ich meine prepareLayout Funktion in und neuen layoutParams setzen. Weil irgendwie nach der Inflation. Die Ansicht ignoriert das Gewicht, das festgelegt wurde. Also musste ich es per Code erzwingen. Vielleicht ist das ein Android-Bug?

0

LayoutWeight zu inneren Komponenten des Layout auf Mutterlinearlayout nur nicht gegeben.

<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

ja, aber das andere linearLayout, das das Gewicht hat, ist eine untergeordnete Ansicht Eltern-Container –

Verwandte Themen