将边距设置为TextView会使其消失

时间:2011-09-20 15:19:04

标签: android layout textview

我是Android devt的初学者(3天前开始)并且在声明View的布局时出现问题。我已经检查了如何以编程方式设置TextView的边距,到目前为止,它们都没有工作。应用布局时,TextView始终消失。

这是我的代码:

TableLayout tView = (TableLayout)findViewById(R.id.AllDocumentsTable);
TableRow trView = buildRow();

TextView tViewProjTitle = buildCell();
tViewProjTitle.setText(doc.project);

TextView tViewDocTitle = buildCell();
tViewDocTitle.setText(doc.document);

trView.addView(tViewProjTitle);
trView.addView(tViewDocTitle);

try {
      tView.addView(trView, i);
    }
catch (Exception e) {
     Log.e("adding tablerow", e.getMessage());
    }

buildRow()..

    private TableRow buildRow(){
        TableRow retRow = new TableRow(this);
        retRow.setBackgroundColor(Color.WHITE);
        TableLayout.LayoutParams rowLayout = new    TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 
                TableLayout.LayoutParams.WRAP_CONTENT);

        rowLayout.setMargins(2, 2, 2, 2);
        retRow.setLayoutParams(rowLayout);
        return retRow;
}

buildCell()..

private TextView buildCell(){
    TextView retTView = new TextView(this);

    retTView.setBackgroundColor(Color.WHITE);
    retTView.setGravity(0);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
            LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(2, 2, 2, 2);

    retTView.setLayoutParams(params);
    return retTView;
}

我的活动布局。

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#ffffff"> 
    <HorizontalScrollView android:id="@+id/horizontalScrollView1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ffffff">   
            <TableLayout android:layout_height="wrap_content" 
                android:id="@+id/AllDocumentsTable" 
                android:layout_width="match_parent" 
                android:background="#ffffff">
                <TableRow android:layout_margin="2dp" 
                    android:background="#000000">
                    <TextView android:text="Test Text1." android:layout_margin="2dp" android:background="#ffffff"></TextView>
                    <TextView android:text="Test Text2" android:layout_margin="2dp" android:background="#ffffff"></TextView>
                </TableRow>
            </TableLayout>      
        </LinearLayout>
    </HorizontalScrollView>    
</LinearLayout>

帮助!!! :)

1 个答案:

答案 0 :(得分:2)

您可以像这样设置边距:

    LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)textview.getLayoutParams();
    lp.topMargin=10;
    lp.leftMargin=10;