【问题标题】:Android TableLayout CustomRowAndroid TableLayout CustomRow
【发布时间】:2012-10-06 07:17:26
【问题描述】:

我正在尝试制作日历,为此我正在使用表格布局,然后我想定义一个包含 TextViews 的自定义行(布局 xml),以便能够在此处访问数据以及我的表格布局示例:

    <TableLayout
  android:id="@+id/appointmentListView"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TableRow 
    android:id="@+id/tableRow1">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        >

        <TextView
            android:id="@+id/unitNul"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00"/>

        <CustomRow><\CustomRow> //this is where I imagined I would have to put my    custom element

    </LinearLayout>


</TableRow>   

然后我会根据他们一天中的时间在正确的行中进行约会(通过切换逻辑)

我的问题是: 如何向每个表格行添加自定义元素? 这是最好的方法吗?

旁注: 我试图制作一个自定义视图元素,但这对我来说似乎有点难以处理。 (如果您知道一个很好的指南来解释这一点,这也是一个选择) 我已经试过了:http://developer.android.com/guide/topics/ui/custom-components.html

【问题讨论】:

  • A/ 使用列表视图 B/ 如果元素数量是动态的,则不能使用 xml。使用 java 和 addView
  • 我在使用列表视图时面临的问题是我需要有一个文本视图来表示左侧的一天中的时间和右侧的类值,这意味着我需要能够指定特定元素的去向。 IE。 12:15 需要转到 textview 显示 12:15 的行,依此类推。 (我实际上需要将我的项目扩展到多行,但一次一个问题)

标签: android android-layout android-widget android-view android-custom-view


【解决方案1】:

像这样创建一个 Xml::

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
            android:id="@+id/tablerows"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/white"
            android:stretchColumns="0,1" >
        </TableLayout>

    </LinearLayout>

</LinearLayout>

然后在活动类中像这样动态添加行和其他视图;

    TableRow row;
    TextView t1, t2;
    int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,(float) 1,             getResources().getDisplayMetrics());
   row = new TableRow(context);
   row.setLayoutParams(new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.FILL_PARENT,
                android.widget.TableRow.LayoutParams.WRAP_CONTENT));
t1 = new TextView(context);
t1.setTypeface(null, 1);
t1.setTextSize(15);
t1.setWidth(50 * dip);
t1.setPadding(20*dip, 0, 0, 0);
row.addView(t1);

然后在你的表中(这里是“myTable”)添加这样的行::

myTable.addView(row, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

【讨论】:

  • 谢谢你,这绝对是我要找的:-D
猜你喜欢
  • 2011-02-24
  • 1970-01-01
  • 2010-12-14
  • 1970-01-01
  • 2011-08-02
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多