2016-11-25 39 views
-1

我的layout_weight正常工作時遇到輕微問題。我正在創建一個自定義的bottomBar。但我不能讓它伸展,因爲我想要它。佈局重量無法按預期工作

底欄視圖

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

這是大容器我加入我的按鈕(項目)來。

底欄項目

<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視圖。 +什麼與downvote。這是一個合法的問題。因爲我自己做了研究,並且注意到動態地我無法使它工作。但硬編碼的看法與其子女工作 –

+0

@Jemil Riahi你可以發佈'容器'視圖和'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 bug?

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是子視圖到父容器 –