我有一个横向线性布局,宽度= match_parent和weightsum=5。如果我插入5个垂直的线性布局,每个width=0和weight=1看起来都和预期的一样,布局都有相同的宽度。如果我在每个width=0和weight=1中只添加2个垂直的空间,那么它们占用的空间比它们应该占用的空间要多。我希望他们也能占据1/5的空间。
也许这是正确的行为,他们占用更多的空间,我理解重量/重量的概念错误。
谢谢你的帮助!
编辑:我尝试添加一些代码
LinearLayout linear=null;
LinearLayout.LayoutParams layoutParams= new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linear=new LinearLayout(getApplicationContext());
linear.setOrientation(LinearLayout.HORIZONTAL);
linear.setLayoutParams(layoutParams);
linear.setPadding(15, 0, 15, 10);
linear.setWeightSum(Float.valueOf(modulo));
//modulo 5 in my example
LinearLayout linear2=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
if(count%modulo!=modulo-1){
lp1.setMargins(0, 0, 15, 0);
} else {
lp1.setMargins(0, 0, 0, 0);
}
linear2.setLayoutParams(lp1);
linear2.setOrientation(LinearLayout.VERTICAL);
我将布局线性2添加到一个循环中,为什么您可以单击run代码:D
发布于 2015-05-12 10:42:17
如果逻辑解决方案不起作用,则用
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="3"/> <!-- or whatever the rest is -->或
View space = new View(this); // NEVER CREATE VIEWS WITH APP CONTEXT!
LinearLayout.LayoutParams spaceParams = new LinearLayout.LayoutParams(0, 0, 3f);
linear.addView(space, spaceParams);注意:在本例中,Space小部件不起作用。
发布于 2015-05-12 10:17:46
尝尝这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:layout_weight="1"/>
</LinearLayout>发布于 2015-05-12 10:20:15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="Your bg"
android:layout_weight="2.5"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="Your bg"
android:layout_weight="2.5"/>
</LinearLayout>平均分配重量。
https://stackoverflow.com/questions/30187744
复制相似问题