5
我想創建內部tablelayout並在那裏添加1行2列,下面的代碼不顯示任何內容,爲什麼?動態創建tablelayout(android)
這裏的主要活動:
public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
TextView tv2=new TextView(this);
tv1.setLayoutParams(params);
tv2.setLayoutParams(params);
tv1.setText("Hello1!");
tv2.setText("Hello2!");
TableLayout layoutINNER = new TableLayout(this);
layoutINNER.setLayoutParams(params);
TableRow tr = new TableRow(this);
tr.setLayoutParams(params);
tr.addView(tv1);
tr.addView(tv2);
layoutINNER.addView(tr);
LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
main.addView(layoutINNER);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/android:main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
UPDATE:
好了,我終於解決了這個問題,它只是需要使用TableRow.LayoutParams與TextViews,而不是LinearLayout.LayoutParams或其他一些
您是否嘗試過使用'TableLayout.LayoutParams',而不是你的'TextViews'和'TableRow'? – kaspermoerch
我最近試過了,它也不起作用 – user1049280
以下是您要找的內容:http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-一個-tablelayout-編程/ – OrhanC1