我读过很多问题,说问题出在 layout_width = 0dip
但我试过了,但无法实现。
我有这个持有人:
<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>
但我的 View 按钮显示仍然显示在左侧...... 有没有一种方法我应该调用 holder 来重新计算它的 child 的权重?
请记住,我是以编程方式创建它们的。
谢谢!
编辑:
我已经有一个这样组成的屏幕:
##################################
# #
# Button | Button | Button #
# #
具有确切的 XML,但不是以编程方式创建,它已经存在于 XML 中。效果很好。
当我以编程方式创建相同的设置时,我得到了这个:
##################################
# #
# Button | Button | Button #
# #
按钮向左对齐,即使重量应该均匀分布。
是否需要调用 LinearLayout
的方法或其他方法来计算权重并更正 View ?
编辑 2:
我只是尝试采用相同的 LinearLayout
并在常规 XML 中插入相同的 Button
3 次。它显示的就像它应该的那样。这绝对与以编程方式创建容器和 3 个按钮有关。
最佳答案
检查你的导入!我遇到了同样的问题,结果我导入了错误的 LayoutParams。我正在导入 android.widget.AbsListView,而不是 android.widget.LinearLayout.LayoutParams,这就是导致问题的原因。
关于以编程方式创建组件时的 Android layout_weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7615648/