[英]Margin TableRow
我瀏覽了所有問過同一件事的人,但沒人能正確回答這個問題。 由於某種原因,我似乎找不到解決方法。 我需要在每個動態制作的TableRows之間添加邊距。 這就是我現在所擁有的。
如您所見,這些行排列在一起,使GUI對用戶不可用。 TableLayout在ScrollView內部。 這是我用來動態創建TextView和按鈕的代碼。 我已經嘗試過使用利潤率,但到目前為止沒有運氣
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
TableLayout tl = (TableLayout) findViewById(R.id.TableLayout1);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT);
/* Margin. */
int leftMargin = 0;
int topMargin = 200;
int rightMargin = 0;
int bottomMargin = 0;
/* Set margins and create new textview. */
tableRowParams.setMargins(leftMargin, topMargin, rightMargin,
bottomMargin);
tr.setLayoutParams(tableRowParams);
TextView encuestasTextView = new TextView(this);
encuestasTextView.setText("Esto es una prueba");
encuestasTextView.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(encuestasTextView);
Button a = new Button(this);
a.setText("Abrir encuesta");
a.setLayoutParams(new TableRow.LayoutParams(width / 3,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(a);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
這是XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Encuestas" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button1"
android:layout_weight="1"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:id="@+id/TableLayout1">
</TableLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:onClick="SyncServer"
android:text="Sync" />
</RelativeLayout>
我想念的是什么?
我認為這:
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
正在清除您之前設置的布局參數。 只需使用tl.addView(tr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.