为什么 layout_weight 和 weightSum 对我不起作用?

杰克杜邦

我有一个关于线性布局的小问题。

假设我有 1 个水平线性布局,其中包含 6 个垂直线性布局。

如果我使用 weightsum=12(水平 ll)和 layout_weight=2(垂直 ll),我可以使所有线性布局具有相同的宽度大小

结果 :

测试

现在我希望“test1”布局比其他布局小 2 或 3 倍。我尝试使用 weightsum=11 和 layout_weigth=1(对于 test1 布局)和 layout_weight=2(对于其他布局)。我无法让它工作,这是我的代码......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="11">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1"
        android:background="@drawable/border"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test1"
            />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="2"
        android:background="@drawable/border"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="2"
        android:background="@drawable/border"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="2"
        android:background="@drawable/border"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="2"
        android:background="@drawable/border"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="2"
        android:background="@drawable/border"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="test"
            />

    </LinearLayout>



</LinearLayout>

我该怎么做?

谢谢你。

拉夫萨哈马德007

您需要使用 : android:layout_width="0dp"for Linearlayoutthe android:layout_weight="1"set as 1。

现在我希望“test1”布局比其他布局小 2 或 3 倍

您可以减少layout weighttest1属性。到 0.5..

使用这种布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="6">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:background="@drawable/border"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test1" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:background="@drawable/border"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/border"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/border"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/border"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/border"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="test" />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>

输出

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么“ NOT IN”和“ NOT EXISTS”不起作用?

为什么我的Javascript和JQuery脚本不起作用?

了解权重和layout_weight时,layout_weight越大,布局中的收缩就越大。

为什么我的CSS代码对我的HTML页眉和页脚不起作用?

使用weightSum和layout_weight时如何在LinearLayout元素上设置最小宽度

为什么NSUserDefaults在我的应用程序和共享扩展之间不起作用?

为什么相同的layout_weight导致Android ImageButton和Button的宽度不同?

为什么类型推导对我的交集和差动调用不起作用?

为什么我的“ if project_form.is_valid()和p_formset.is_valid()”不起作用?

RecyclerView中项目的layout_weight和layout_gravity不起作用

为什么我的按钮对php和javascript不起作用,而我的其他按钮对呢?

C ++为什么我在int和str.length()之间进行的比较不起作用?

如果我使用变量,为什么这个jQuery动画和附加操作不起作用?

固定宽度的Android Layout_Weight不起作用

为什么我的删除/销毁方法和链接不起作用?

想删除星号和前面的字符并跟随它,为什么我的代码不起作用

Android Layout_Weight属性不起作用

验证失败后,为什么我的redirectAttributes.addFlashAttribute和重新填充的数据不起作用?

为什么在我的Ember.ComputedProperty中“或”有效,而“和”却不起作用?

为什么 Animation.cancel() 和 View.clearAnimation() 在我的情况下不起作用?

JQuery、PHP 和 mySQL 帮助。我不明白为什么这不起作用

Python 和 BeautifulSoup:为什么我的 if 条件不起作用

如果我离开和返回视图控制器,为什么 viewWillTransition(to size:...) 不起作用?

如果我尝试在 Matlab 中链接日期时间和纬度,为什么 linkaxis 不起作用?

为什么 layout_weight 在 TextInputLayout 的孩子中不起作用?

为什么某些 Bootstrap 类和 CSS 属性在我的代码中不起作用?

为什么我的最大化和 DragMove 在我的 MouseDown 事件中不起作用?

为什么 Python 中的 sin 和 cos 函数对我不起作用?

为什么我重定向到我的前端不起作用(Express 和 React)