Android tablelayout视图未出现 [英] Android tablelayout view does not appear

查看:38
本文介绍了Android tablelayout视图未出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

android编程的新手.

New to android programming.

我有一个布局XML,我想向其中添加一个列表格式(包含数据库中的项目),格式为表格.

I have a layout XML, to which I want to add a list (with items from a database), formatted as a table.

这是代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.edit_cat);

        DatabaseHandler db = new DatabaseHandler(this);
        List<Category> allCategories = db.getAllCategories();

        //Get the main layout from XML
        LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat);

        //Create table layout for the categories
        TableLayout catList = new TableLayout(this);

        //Iterate the categories, and organize in tables
        for (Category category : allCategories) {
            TableRow tr = new TableRow(this);

            //Display category name
            TextView name = new TextView(this);
            name.setLayoutParams(new LayoutParams(                      
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            name.setText(category.getName());


            //Display category type
            TextView type = new TextView(this);
            type.setText(category.getType());
            type.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            //Display edit button
            Button btnEdit = new Button(this);
            btnEdit.setText(getResources().getString(R.string.edit));

            btnEdit.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            //Display delete button
            Button btnDelete = new Button(this);
            btnDelete.setText(getResources().getString(R.string.delete));

            btnDelete.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));


            tr.addView(name);
            tr.addView(type);
            tr.addView(btnEdit);
            tr.addView(btnDelete);

            catList.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }

        mainLayout.addView(catList);


    }

这不会向布局添加任何内容.任何想法为什么这不起作用?

This does not add anything to the layout. Any ideas why this does not work?

在此处添加了xml代码:

Added the xml code here:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_cat"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">

<TextView style="@style/pageHeader"
  android:text="@string/catEditor"/>

<Button
    android:id="@+id/btnNewCategory"
    android:text="@string/newCategory"
    android:textSize="11pt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onBtnClicked"/>
<Button
    android:id="@+id/back"
    android:text="@string/back"
    android:textSize="11pt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="onBtnClicked"/>

</LinearLayout>

推荐答案

好的,我想我有一个答案.

Ok, I think I have an answer for you.

在指定LayoutParams时,您需要更加具体.尽管它不会引发编译异常,但是如果您未指定LayoutParams的正确父类,则不会显示任何内容.

You need to be more specific when you are specifying LayoutParams. While it doesn't throw a compile exception, if you don't specify the correct parent class of the LayoutParams, nothing will show up.

指定布局参数时,需要使用对象将要到达的容器的类.如果要设置在LinearLayout内的textview的布局参数,则类似地,如果要创建要在TableRow内的textview,则可以使用 new LinearLayout.LayoutParams(...)需要使用 TableRow.LayoutParams(...)

When you specify layout params, you need to use the class of the container where the object is going. If you are setting the layout paramters of a textview that is going inside a LinearLayout, you would use new LinearLayout.LayoutParams(...) similarily if you are creating a textview to go inside a TableRow, you need to use TableRow.LayoutParams(...)

下面是完整的代码,做了一些小的修改,因为我没有数据库设置.

Below is the full code with minor modifications because I didn't have a db setup.

    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.edit_cat);

    //Create table layout for the categories        
    TableLayout catList = new TableLayout(this);

    //Set the table layout to Match parent for both width and height
    // Note: We use LinearLayout.LayoutParams because the TableLayout is going inside a LinearLayout

    catList.setLayoutParams(
        new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    //Iterate the categories, and organize in tables      
    for(int i = 0; i < 5; i++)
    {
        TableRow tr = new TableRow(this);
        /* Since the table row is going inside a table layout, we specify the parameters using TableLayout.LayoutParams */
        tr.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

        //Display category name
        TextView name = new TextView(this);

        /* Since the TextView is going inside a TableRow, we use new TableRow.LayoutParams ... */
        name.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        name.setText("Test Row - " + i);
        name.setBackgroundColor(Color.RED);


        //Display category type
        TextView type = new TextView(this);
        type.setText("Type Row - " + i);
        type.setBackgroundColor(Color.GREEN);
        type.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        //Display edit button
        Button btnEdit = new Button(this);
        btnEdit.setText("Edit");

        btnEdit.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT));

        //Display delete button
        Button btnDelete = new Button(this);
        btnDelete.setText("Delete");

        btnDelete.setLayoutParams(new TableRow.LayoutParams(
                 TableRow.LayoutParams.FILL_PARENT,
                 TableRow.LayoutParams.WRAP_CONTENT));


        tr.addView(name);
        tr.addView(type);
        tr.addView(btnEdit);
        tr.addView(btnDelete);

        catList.addView(tr);
    }


    mainLayout.addView(catList);

这篇关于Android tablelayout视图未出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆