Android - 无法在tablelayout中看到行

时间:2014-04-20 10:42:57

标签: android xml layout

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/MainLayout">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/MainTable"
        android:layout_marginTop="5dp">
    </TableLayout>

</ScrollView>
</RelativeLayout>

这是我的代码

     private void showMarkets () {

    markets = marketAdapter.getAllMarkets();

    TableRow newRow = new TableRow(getApplicationContext());
    //TableRow newRow = new TableRow(this);

    newRow.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
    newRow.setGravity(Gravity.RIGHT);

    for (int i = 0; i < marketAdapter.headingTitle.length; i++){
        newRow.addView(createView(marketAdapter.headingTitle[i], 20, android.R.color.white));
        Log.e("title", marketAdapter.headingTitle[i]);
    }

    table.addView(newRow);
}
public TextView createView (String columnName, int size, int columnColor)
    {
        TextView textView = new TextView(getApplicationContext());
        textView.setText(columnName);
        textView.setTextSize(size);
        textView.setTextColor(getResources().getColor(columnColor));
        textView.setPadding(5, 10, 5, 10);
        textView.setGravity(Gravity.CENTER);

        return textView;
    }

在互联网上尝试了很多例子,但没有什么效果很好,我想知道我的问题在哪里,我真的很抱歉我的英语不好我希望你能帮助我

添加我的创建视图功能

2 个答案:

答案 0 :(得分:0)

ScrollView应该是父元素。您不需要相对布局。试试这个:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/MainTable"
    android:layout_marginTop="5dp">
        <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
            <TextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="TextView in a row"/>
        </TableRow>
</TableLayout>

答案 1 :(得分:0)

您需要将布局参数设置为新行:

  private void showMarkets () {

    markets = marketAdapter.getAllMarkets();

    TableRow newRow = new TableRow(getApplicationContext());
    //TableRow newRow = new TableRow(this);

    newRow.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_dark));
    newRow.setGravity(Gravity.RIGHT);

    for (int i = 0; i < marketAdapter.headingTitle.length; i++){

    //add layout params to view inside layout row as well
        newRow.addView(createView(marketAdapter.headingTitle[i], 20, android.R.color.white),new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)));
        Log.e("title", marketAdapter.headingTitle[i]);
    }
    newRow.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    table.addView(newRow);
}