简体   繁体   English

替代Android中的嵌套权重?

[英]Alternative for nested weights in Android?

I want to display eleven rows containing four TextView items each, that's a total of 44 textviews in one screen. 我想显示包含四个TextView项目的11行,在一个屏幕上总共有44个文本视图。

I didn't want the difference in screen sizes of different handsets to cause issues with this so I thought of using nested weights. 我不希望不同手机的屏幕尺寸差异导致问题,所以我想到使用嵌套权重。 I'd have a LinearLayout with a horizontal orientation, containing four TextView items with their weights adjusted so they'd fit properly on the screen in one line. 我有一个水平方向的LinearLayout ,包含四个TextView项目,其权重已调整,因此它们可以在一行中正确显示在屏幕上。 I'd have eleven such LinearLayout items nested within a parent LinearLayout with a vertical orientation and I'd adjust the weights of the inner LinearLayout items so that they would all fit on the screen. 我有11个这样的LinearLayout项嵌套在具有垂直方向的父LinearLayout ,我将调整内部LinearLayout项的权重,以便它们都适合屏幕。

However, this does not seem to work. 但是,这似乎不起作用。 It says that nested weights are bad for performance. 它说嵌套权重对性能不利。

How do I solve this issue? 我该如何解决这个问题?

If you want to add the text views dynamically you can go for listview with custom adapters. 如果要动态添加文本视图,可以使用自定义适配器进行列表视图 If you want it to be static, then you can go for tablelayout with each table rows having four textviews each. 如果您希望它是静态的,那么您可以使用tablelayout ,每个表行每个具有四个textview。 If you are not satisfied, I can give you an example layout also.. 如果你不满意,我也可以给你一个示例布局..

Sample TableLayout 示例TableLayout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*">

    <TableRow>
      <!-- Column 1 -->
      <TextView
         android:id="@+id/tbl_txt1"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 1" />

      <!-- Column 2 -->
      <TextView
         android:id="@+id/tbl_txt2"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 2" />

      <!-- Column 3 -->
      <TextView
         android:id="@+id/tbl_txt3"
         android:layout_width="0dip"
         android:layout_height="wrap_content"
         android:background="@color/red"
         android:textColor="@color/white"
         android:padding="10dip"
         android:layout_margin="4dip"
         android:layout_weight="1"
         android:text="Column 3" />
    </TableRow>
</TableLayout>

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

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