2012-02-17 74 views
1

我的應用程序中的一個活動(我的第一次嘗試)生成多個錶行,每行表中包含一對EditText和一個Button。但是,該按鈕始終顯示爲未對齊,導致this problem - 刪除按鈕略微向下移動。Android:在TableRow中排列視圖以編程方式

的XML佈局的相關部分如下:

<ScrollView 
    android:id="@+id/countEditList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TableLayout 
    android:id="@+id/countEditLayout" 
    android:stretchColumns="*" 
    android:gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <!-- insert the new rows here as they are created --> 
    </TableLayout> 
</ScrollView> 

從一個數據庫遊標創建每一行代碼執行以下操作:

TableRow row = new TableRow(this); 

EditText title = new EditText(this); 
title.setHorizontallyScrolling(true); 
title.setGravity(Gravity.CENTER); 
title.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

EditText counting = new EditText(this); 
counting.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
counting.setGravity(Gravity.CENTER); 

Button deleteCount = new Button(this); 
deleteCount.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
deleteCount.setText(R.string.deleteCount); 
deleteCount.setOnClickListener(this); 
deleteCount.setGravity(Gravity.CENTER); 

row.addView(title); 
row.addView(counting); 
row.addView(deleteCount); 
layout.addView(row,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // Earlier: layout = (ViewGroup) findViewById(R.id.countEditLayout); 

我懷疑,我應該以某種方式是設置EditTexts和Button的layout_gravity,但我無法解決如何做到這一點 - 任何人都可以提供任何建議嗎?

回答

1

你可以使用一些填充調整編輯文本定位:

title.setPadding(3, 3, 3, 3); 
counting.setPadding(3, 3, 3, 3); 
+0

感謝您的答覆。不幸的是,這似乎並沒有改變行內deleteCount按鈕的相對對齊。我是否應該爲此使用其他某種佈局,而不是表格? – knirirr 2012-02-17 14:17:44

+0

終於明白了! row.setGravity(Gravity.CENTER);看起來像它的伎倆。 – knirirr 2012-02-17 14:26:12