繁体   English   中英

Android布局的重量故障?

[英]Android layout weight glitches?

在Android应用程序上使用layout_weight时,出现一些奇怪的故障。 为了确保我没有浪费任何人的时间。 当我有三个自定义视图并赋予两个视图权重1和一个视图权重2时,权重为2的视图应该是最大的视图,对吗? 因为那不是我发生的事。

几个例子与行为:

  • 1:2:1-中间的视图完全消失。
  • 1.1:1.1:1-前两个视图略小于第三视图。 (这是我目前用来获取所需布局的内容。)

码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="25dp"
        android:layout_marginEnd="25dp"
        android:orientation="vertical"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <CUSTOMVIEW
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="12.5dp"
            android:layout_weight="1.1" />

        <CUSTOMVIEW
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12.5dp"
            android:layout_marginBottom="12.5dp"
            android:layout_weight="1.1"/>

        <CUSTOMVIEW
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12.5dp"
            android:layout_weight="1"/>
    </LinearLayout>

注意:我尝试将customviews的宽度和高度设置为0dp ,但是它们完全消失了。

使高度0dp和宽度wrap_content 干得好B.Cakir。

如果要在视图中使用layout_weight,则必须在父视图中使用weight_sum。 例如:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="25dp"
    android:layout_marginEnd="25dp"
    android:orientation="vertical"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:weightSum="3.2">

    <CUSTOMVIEW
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12.5dp"
        android:layout_weight="1.1" />

    <CUSTOMVIEW
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12.5dp"
        android:layout_marginBottom="12.5dp"
        android:layout_weight="1.1"/>

    <CUSTOMVIEW
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12.5dp"
        android:layout_weight="1"/>
</LinearLayout>

请注意,weight_sum属性是1.1 + 1.1 + 1 = 3.2的结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM