2011-09-30 54 views
2

我讀過很多問題,說問題是layout_width = 0dip,但我已經嘗試過並且無法完成此項工作。以編程方式創建組件時的Android layout_weight

我有這樣的持有人:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/linearLayout1" 
    android:orientation="horizontal" 
    android:weightSum="3" 
    android:padding="10dip" 
    android:fadingEdge="none" 
    android:background="@drawable/bg_filters"> 
</LinearLayout> 

而這個諧音:

<Button 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:layout_width="0dip" 
    android:layout_height="40dip" 
    android:padding="4dip" 
    android:gravity="center" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:layout_marginRight="15dip" 
    android:background="@drawable/bg_filter_bt_active" 
    android:textColor="@color/app_color_all"> 
</Button> 

,但我的意見按鈕,顯示仍然顯示分組向左...... 有沒有一種方法,我應該請持有人重新計算其子女的體重?

請記住,我以編程方式創建它們。

謝謝!

編輯:

我已經有這個成分的屏幕:

################################## 
#        # 
# Button | Button | Button # 
#        # 

與精確的XML,但而不是編程方式創建,它是在XML已經存在。它完美的作品。

當我創建相同的設置,但編程,我得到這個:

################################## 
#        # 
# Button | Button | Button  # 
#        # 

按鈕贊同向左,即使壽重量應均勻擺放。 是否有方法或我需要致電LinearLayout來計算重量並更正視圖?

編輯2:

我只是想採取這一相同LinearLayout並插入裏面的相同Button 3次定期XML。它顯示就像它應該。這絕對是以編程方式創建持有者和3個按鈕的。

+1

您不能有0寬度或高度。你想要做什麼? – asenovm

+2

你想要做什麼?你想讓你的按鈕成爲佈局的寬度嗎?您的按鈕是否在佈局內? – digerati32

+0

@iLate:寬度或高度爲0是可能的,並與'layout_weight'屬性結合使用。例如,請參閱[此問題]的答案(http://stackoverflow.com/questions/4986861/android-layout-weight)。 – 2011-09-30 21:41:15

回答

0

檢查您的導入!我有同樣的確切問題,結果我導入了錯誤的LayoutParams。我導入了android.widget.AbsListView,而不是android.widget.LinearLayout.LayoutParams,導致了這個問題。

0

你是否嘗試強制視圖通過invalidate()或refreshDrawableState()重繪自己?
你可以將代碼發佈到你以編程方式創建這些按鈕的位置嗎?