2011-09-30 48 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()重绘自己?
你可以将代码发布到你以编程方式创建这些按钮的位置吗?