[英]TableRow not being displayed in TableLayout
任何人都可以看到以下問題嗎:
if (mViewPager.getCurrentItem() == 3){
TableLayout tbl = (TableLayout)findViewById(R.id.tblHistory);
if (tbl.getChildCount() != mExceptions.size()){
for (int i = tbl.getChildCount(); i < mExceptions.size(); i++){
GPSException e = mExceptions.get(i);
//TableRow
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tr.setBackgroundColor(Color.BLACK);
//DateTime
TextView tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(e.DateTime);
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Speed
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(Float.toString(e.Speed));
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Bearing
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(Double.toString(e.Degrees));
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
//Exceptions
tx = new TextView(this);
tx.setLayoutParams(new android.view.ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tx.setText(e.ExceptionString());
tx.setTextAppearance(getApplicationContext(), android.R.attr.textAppearanceMedium);
tx.setTextColor(Color.BLACK);
tx.setBackgroundColor(Color.WHITE);
tr.addView(tx);
tbl.addView(tr, 0);
}
tbl.invalidate();
tbl.refreshDrawableState();
}
}
我的類實現了LocationListener,並且在onLocationChanged事件中觸發了以上代碼。 目的是為每個排隊的“異常”將TableRow添加到TableLayout。 每行插入位置0。
我嘗試了一切,除了會出現的正確方法之外!
我的布局如下(暫時不使用資源,但是一旦我解決了這個問題,資源便會被使用-對於所有鷹眼的觀眾!):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TableRow android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dt/Tm"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Spd"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Brg"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ex"></TextView>
</TableRow>
</TableLayout>
沒有引發任何異常,如果我在for循環中敬酒了一條短消息,它的確會出現。
如果您需要更多信息,那不是問題。
謝謝。
tbl.addView(tr, 0);
我認為您需要給該布局參數,而不是0。也許嘗試一下?
tbl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
完成所有這些操作后,刪除android.view.ViewGroup.LayoutParams
限定符(意味着每個TextView
的布局參數現在為TableRow.LayoutParams
)已解決了問題。 為什么? 我不知道......
也許TextView的布局參數必須與TableRow有關?!! 在這里刮。 無論如何,問題解決了:-(
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.