Android自定义组件不工作

Android自定义组件不工作,android,Android,我正试图通过在Android中编码来开发一个自定义表。我正在尝试创建以下布局 <LinearLayout> <Table> <TableRow> Title </TableRow> <TableRow> Header </TableRow> </Table> <ScrollView> <Table>

我正试图通过在Android中编码来开发一个自定义表。我正在尝试创建以下布局

<LinearLayout>
    <Table>
        <TableRow> Title </TableRow>
        <TableRow> Header </TableRow>
    </Table>
    <ScrollView>
        <Table>
            <TableRow> Row1 </TableRow>
            <TableRow> Row2 </TableRow>
        </Table>
    </ScrollView>
</LinearLayout>
建造师

    public TableView(Activity context, String title, String[] headers,
        List<String[]> data) {
    super(context);
    _data = data;
    _title = title;
    _context = context;
    _headers = headers;
}
在执行这段代码时,我看到的只是标题和标题栏,而不是内容。我错过了什么

任何指针或链接都会帮助我

@编辑:对于有相同问题的人来说,这里的解决方案对我来说很有效。 我创建的XML文件:

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="true"
    android:stretchColumns="true" >

    <TableRow
        android:id="@+id/headerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/dataRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <HorizontalScrollView
                    android:id="@+id/horizontalScrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/linearLayout2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >

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

                            <TableRow
                                android:id="@+id/actualDataRow"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" >

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Large Text"
                                    android:textAppearance="?android:attr/textAppearanceLarge" />
                            </TableRow>
                        </TableLayout>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>
        </ScrollView>
    </TableRow>

    <TableRow
        android:id="@+id/footerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>
</TableLayout>

</LinearLayout>
正在创建自定义组件的类

import in.ns.custom.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CustomView extends LinearLayout {

private Context _context;
private LayoutInflater _inflater;
private View _view;

public CustomView(Context context) {
    super(context);
    _context = context;
    _inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    _view = _inflater.inflate(R.layout.custom, this);
    init();
}

public View getView() {
    return _view;
}

private void init() {
    TableLayout tableLayout = (TableLayout) _view.findViewById(R.id.dataTableLayout);
    TableRow tableRow ; //(TableRow) tableLayout.getChildAt(0);
    
    tableLayout.removeAllViews();
    
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    for(int i = 0; i < 32; i++) {
        tableRow = new TableRow(_context);
        
        tv1 = new TextView(_context);
        tv1.setText("Success1 ");
        
        tableRow.addView(tv1);
        
        tv2 = new TextView(_context);
        tv2.setText("Success2 ");

        tableRow.addView(tv2);
        
        tv3 = new TextView(_context);
        tv3.setText("Success1 ");
        
        tableRow.addView(tv3);
        
        tv4 = new TextView(_context);
        tv4.setText("Success2 ");

        tableRow.addView(tv4);
        
        tv5 = new TextView(_context);
        tv5.setText("Success1 ");
        
        tableRow.addView(tv5);
        
        tv6 = new TextView(_context);
        tv6.setText("Success2 ");

        tableRow.addView(tv6);
        
        tableLayout.addView(tableRow);          
    }
    
    tableRow = new TableRow(_context);
    
    TextView tv11 = new TextView(_context);
    tv11.setText("Success11 ");
    
    tableRow.addView(tv11);
    
    TextView tv21 = new TextView(_context);
    tv21.setText("Success21 ");
    tableRow.addView(tv21);
    
    tableLayout.addView(tableRow);
}
}
在.ns.custom.R中导入;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.LinearLayout;
导入android.widget.TableLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类CustomView扩展了LinearLayout{
私人语境(private Context)(私人语境);;
私人充气机;
私有视图(u视图),;
公共自定义视图(上下文){
超级(上下文);
_上下文=上下文;
_充气器=(LayoutInflater)\u context.getSystemService(context.LAYOUT\u充气器\u SERVICE);
_视图=_充气器充气(R.layout.custom,this);
init();
}
公共视图getView(){
返回视图;
}
私有void init(){
TableLayout=(TableLayout)\ u view.findviewbyd(R.id.dataTableLayout);
TableRow TableRow;//(TableRow)tableLayout.getChildAt(0);
tableLayout.removeallview();
文本视图tv1;
文本视图tv2;
文本视图tv3;
文本视图tv4;
文本视图tv5;
文本视图tv6;
对于(int i=0;i<32;i++){
tableRow=新的tableRow(_上下文);
tv1=新文本视图(_上下文);
tv1.setText(“成功1”);
tableRow.addView(tv1);
tv2=新文本视图(_上下文);
tv2.setText(“Success2”);
tableRow.addView(tv2);
tv3=新文本视图(_上下文);
tv3.setText(“成功1”);
tableRow.addView(tv3);
tv4=新文本视图(_上下文);
tv4.setText(“成功2”);
tableRow.addView(tv4);
tv5=新文本视图(_上下文);
tv5.setText(“成功1”);
tableRow.addView(tv5);
tv6=新的文本视图(_上下文);
tv6.setText(“Success2”);
tableRow.addView(tv6);
tableLayout.addView(tableRow);
}
tableRow=新的tableRow(_上下文);
TextView tv11=新的TextView(_上下文);
tv11.setText(“Success11”);
tableRow.addView(tv11);
TextView tv21=新的TextView(_上下文);
tv21.setText(“Success21”);
tableRow.addView(tv21);
tableLayout.addView(tableRow);
}
}

您可以使用
XML
并在调用构造函数后将其膨胀,而不是从代码中创建视图

您的代码中有一些错误:

  • \u context.setContentView(linearLayout)
那不是正确的做法。你不应该到处传递
活动。
从
活动构建视图
并从中设置内容视图

  • 您永远不会向实例添加子对象

任何地方都没有
this.addChild()
。创建的所有内容都不会添加到
TableLayout

而不是从代码创建视图,您可以使用
XML
并在调用构造函数后将其膨胀

您的代码中有一些错误:

  • \u context.setContentView(linearLayout)
那不是正确的做法。你不应该到处传递
活动。
从
活动构建视图
并从中设置内容视图

  • 您永远不会向实例添加子对象

任何地方都没有
this.addChild()
。创建的所有内容都不会添加到
表格布局中

@Macarse
谢谢您的回复。我使用XML和充气方法解决了我的问题。
@Macarse
谢谢您的回复。我使用XML和充气方法来解决我的问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="true"
    android:stretchColumns="true" >

    <TableRow
        android:id="@+id/headerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/dataRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <HorizontalScrollView
                    android:id="@+id/horizontalScrollView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <LinearLayout
                        android:id="@+id/linearLayout2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal" >

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

                            <TableRow
                                android:id="@+id/actualDataRow"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" >

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="Large Text"
                                    android:textAppearance="?android:attr/textAppearanceLarge" />
                            </TableRow>
                        </TableLayout>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>
        </ScrollView>
    </TableRow>

    <TableRow
        android:id="@+id/footerRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>
</TableLayout>

</LinearLayout>
import in.ns.views.CustomView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout.LayoutParams;

public class CustomComponentActivity extends Activity implements OnClickListener {
Button viewButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    viewButton = (Button) findViewById(R.id.button1);
    viewButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    CustomView cv = new CustomView(this);
    LayoutParams LP_FP_FP = new LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    //this.addView(cv);
    //this.addContentView(cv.getView(), LP_FP_FP);
    setContentView(cv.getView());
    Log.d("custom", "added view");
}
}
import in.ns.custom.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class CustomView extends LinearLayout {

private Context _context;
private LayoutInflater _inflater;
private View _view;

public CustomView(Context context) {
    super(context);
    _context = context;
    _inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    _view = _inflater.inflate(R.layout.custom, this);
    init();
}

public View getView() {
    return _view;
}

private void init() {
    TableLayout tableLayout = (TableLayout) _view.findViewById(R.id.dataTableLayout);
    TableRow tableRow ; //(TableRow) tableLayout.getChildAt(0);
    
    tableLayout.removeAllViews();
    
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    for(int i = 0; i < 32; i++) {
        tableRow = new TableRow(_context);
        
        tv1 = new TextView(_context);
        tv1.setText("Success1 ");
        
        tableRow.addView(tv1);
        
        tv2 = new TextView(_context);
        tv2.setText("Success2 ");

        tableRow.addView(tv2);
        
        tv3 = new TextView(_context);
        tv3.setText("Success1 ");
        
        tableRow.addView(tv3);
        
        tv4 = new TextView(_context);
        tv4.setText("Success2 ");

        tableRow.addView(tv4);
        
        tv5 = new TextView(_context);
        tv5.setText("Success1 ");
        
        tableRow.addView(tv5);
        
        tv6 = new TextView(_context);
        tv6.setText("Success2 ");

        tableRow.addView(tv6);
        
        tableLayout.addView(tableRow);          
    }
    
    tableRow = new TableRow(_context);
    
    TextView tv11 = new TextView(_context);
    tv11.setText("Success11 ");
    
    tableRow.addView(tv11);
    
    TextView tv21 = new TextView(_context);
    tv21.setText("Success21 ");
    tableRow.addView(tv21);
    
    tableLayout.addView(tableRow);
}
}