【问题标题】:Android weightsum not working as expectedAndroid weightsum 无法按预期工作
【发布时间】:2015-12-09 16:43:41
【问题描述】:

我有一个宽度 = match_parent 和 weightsum=5 的水平线性布局。 如果我插入 5 个垂直线性布局,每个宽度 = 0 和重量 = 1,一切看起来都符合预期,每个布局都具有相同的宽度。 如果我只添加 2 个垂直,每个宽度 = 0 和重量 = 1,它们会占用比应有的空间更多的空间。我预计它们也会占用 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 添加到线性 为什么可以点击运行代码:D

【问题讨论】:

  • 能否也发个截图?
  • 这对于布局权重属性是不可能的。使用布局权重,视图都相等并填满。
  • 但是我将 weightsum 设置为 5 并将布局的权重设置为 1,所以它们都应该占用 1/5 的空间?
  • WeightSum 在您将布局初始化为 Fill_PARENT 时正常工作。我可以在你的代码中看到,你已经用高度 WRAP_CONTENT 初始化了你的线性布局
  • 但是我用权重作为宽度,为什么高度会影响结果?

标签: android android-linearlayout android-layout-weight


【解决方案1】:

试试这个

    <?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>

【讨论】:

  • 身高可以用weightSum吗?
【解决方案2】:

如果逻辑解决方案不起作用,请填写其余部分

<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 小部件在这种情况下不起作用。

【讨论】:

  • 我用这个替换了所有的 Appcontexts。空间的东西也不起作用,总是一样的结果:(
  • Hmm.. 首先尝试1 的高度(这是第二个布局参数参数),如果这不起作用尝试View 而不是Space,如果这不起作用尝试两者结合,如果这不起作用......无论如何请告诉我。
  • 因此,如果我使用除了权重为 3 的空间之外的视图,它可以工作。我真的不喜欢这样的“解决方法”,但至少它可以满足我的需求。非常感谢您的帮助。如果您编辑答案,我会将其标记为正确
【解决方案3】:
<?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>

平均分配重量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多