【问题标题】:How to change respectively the width of a TextView in a row according to TextView number?如何根据TextView编号分别改变一行TextView的宽度?
【发布时间】:2016-08-19 11:05:07
【问题描述】:

因此,根据我的程序流程,我的活动中有一个行视图,该行视图在程序生命周期中由 TextViews 自定义填充。问题是当 TextViews 生成时,它们彼此相邻出现(通过我设置的布局参数)。

E.x

TextView_1 TextView_2 TextView_3

我想要的是,根据生成的视图数量(每次活动开始时,都会生成特定数量的TextView)强> 在活动中采用特定且相等的宽度,如下所示:

我怎样才能成功呢?

感谢每一个小小的帮助。

【问题讨论】:

    标签: android layout textview


    【解决方案1】:

    你必须使用权重:

    例如

     <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:weightSum="3"
        android:orientation="horizontal"
        android:layout_gravity="center">
    
        <TextView
        android:layout_height="wrap_content"       
        android:layout_weight="1"
        android:layout_width="0dp"/>
    
        <TextView
        android:layout_height="wrap_content"       
        android:layout_weight="1"
        android:layout_width="0dp"/>
    
        <TextView
        android:layout_height="wrap_content"       
        android:layout_weight="1"
        android:layout_width="0dp"/>
    
    
    </LinearLayout>
    

    权重分配很重要。

    如果你放 10,总重量必须是 10。

    如果你放了 3 总重量一定是三个。

    换句话说 1 + 1 + 1 = 3 所以处理了 3 个元素的宽度。

    或者还有 3.3 + 3.3 + 3.3 = 9.9

    【讨论】:

    • 如果不想留任何空白,也可以避免使用 weightSum。
    • 感谢您的回答!那么,如果不使用 weightSum,我只是在 view 中相应地使用 weight?
    • 是的,你只能使用 layout_weight
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2011-12-25
    • 1970-01-01
    相关资源
    最近更新 更多