我正在尝试通过Android中的编码开发自定义表格。我正在尝试创建以下布局
<LinearLayout>
<Table>
<TableRow> Title </TableRow>
<TableRow> Header </TableRow>
</Table>
<ScrollView>
<Table>
<TableRow> Row1 </TableRow>
<TableRow> Row2 </TableRow>
</Table>
</ScrollView>
</LinearLayout>
我在这里看到的帖子很少,this非常接近我想要的但不完全。
类声明是
public class TableView extends TableLayout implements OnClickListener
构造
public TableView(Activity context, String title, String[] headers,
List<String[]> data) {
super(context);
_data = data;
_title = title;
_context = context;
_headers = headers;
}
初始化代码
LinearLayout linearLayout = new LinearLayout(_context);
// Create a new Table Layout
TableLayout table = new TableLayout(_context);
// Set stretch and shrink for all the columns in the table
table.setStretchAllColumns(true);
table.setShrinkAllColumns(false);
// Add the title row to the table
TableRow rowTitle = new TableRow(_context);
rowTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow rowHeader = new TableRow(_context);
// title column/row
TextView title = new TextView(_context);
title.setText("User Details");
// Set properties of title.
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
title.setGravity(Gravity.CENTER);
title.setTypeface(Typeface.SERIF, Typeface.BOLD);
// Create a table layout params and add the param to span 6 columns
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 6;
rowTitle.addView(title, params);
// labels column
TextView headerLabel1 = new TextView(_context);
headerLabel1.setText("Company Name");
headerLabel1.setTypeface(Typeface.DEFAULT_BOLD);
rowHeader.addView(headerLabel1);
TextView headerLabel2 = new TextView(_context);
headerLabel2.setText("User Type");
headerLabel2.setTypeface(Typeface.DEFAULT_BOLD);
rowHeader.addView(headerLabel2);
TextView headerLabel3 = new TextView(_context);
headerLabel3.setText("Active");
headerLabel3.setTypeface(Typeface.DEFAULT_BOLD);
rowHeader.addView(headerLabel3);
TextView headerLabel4 = new TextView(_context);
headerLabel4.setText("Mobile #");
headerLabel4.setTypeface(Typeface.DEFAULT_BOLD);
rowHeader.addView(headerLabel4);
TextView headerLabel5 = new TextView(_context);
headerLabel5.setText("Login Id");
headerLabel5.setTypeface(Typeface.DEFAULT_BOLD);
rowHeader.addView(headerLabel5);
TextView headerLabel6 = new TextView(_context);
headerLabel6.setText("TimeZone");
headerLabel6.setTypeface(Typeface.DEFAULT_BOLD);
rowHeader.addView(headerLabel6);
table.addView(rowTitle);
table.addView(rowHeader);
linearLayout.addView(table);
// Add Data to row
ScrollView dataScroll = new ScrollView(_context);
LayoutParams dataParams = LP_WC_WC;
dataParams.height = LayoutParams.FILL_PARENT;
dataParams.width = LayoutParams.FILL_PARENT;
dataScroll.setLayoutParams(dataParams);
TableLayout rowTable = new TableLayout(_context);
rowTable.setLayoutParams(LP_FP_WC);
TableRow row = new TableRow(_context);;
TextView dataText1 = new TextView(_context);
dataText1.setText("IBM");
dataText1.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(dataText1);
TextView dataText2 = new TextView(_context);
dataText2.setText("Administrator");
dataText2.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(dataText2);
TextView dataText3 = new TextView(_context);
dataText3.setText("Yes");
dataText3.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(dataText3);
TextView dataText4 = new TextView(_context);
dataText4.setText("123-123-1234");
dataText4.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(dataText4);
TextView dataText5 = new TextView(_context);
dataText5.setText("testcompany");
dataText5.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(dataText5);
TextView dataText6 = new TextView(_context);
dataText6.setText("UTC+5:30");
dataText6.setTypeface(Typeface.DEFAULT_BOLD);
row.addView(dataText6);
rowTable.addView(row);
dataScroll.addView(rowTable);
linearLayout.addView(dataScroll);
_context.setContentView(linearLayout);
在执行此代码时,我发现只有标题和标题栏不是内容。我错过了什么?
任何指针或链接都会对我有帮助。
我创建的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);
}
}
答案 0 :(得分:1)
您可以使用XML
并在调用构造函数后对其进行充气,而不是从代码中创建视图。
您的代码中存在一些错误:
_context.setContentView(linearLayout);
这不是正确的做法。你不应该传递Activity
。
从Activity
构建视图并从中设置内容视图。
任何地方都没有this.addChild()
。创建的所有内容都不会添加到TableLayout
。