首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android称重未按预期工作

Android称重未按预期工作
EN

Stack Overflow用户
提问于 2015-05-12 10:10:17
回答 3查看 1.7K关注 0票数 2

我有一个横向线性布局,宽度= match_parent和weightsum=5。如果我插入5个垂直的线性布局,每个width=0和weight=1看起来都和预期的一样,布局都有相同的宽度。如果我在每个width=0和weight=1中只添加2个垂直的空间,那么它们占用的空间比它们应该占用的空间要多。我希望他们也能占据1/5的空间。

也许这是正确的行为,他们占用更多的空间,我理解重量/重量的概念错误。

谢谢你的帮助!

编辑:我尝试添加一些代码

代码语言:javascript
复制
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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-12 10:42:17

如果逻辑解决方案不起作用,则用

代码语言:javascript
复制
<View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="3"/> <!-- or whatever the rest is -->

代码语言:javascript
复制
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小部件不起作用。

票数 0
EN

Stack Overflow用户

发布于 2015-05-12 10:17:46

尝尝这个

代码语言:javascript
复制
    <?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>
票数 1
EN

Stack Overflow用户

发布于 2015-05-12 10:20:15

代码语言:javascript
复制
<?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>

平均分配重量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30187744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档