1

我有 main.xml :

     <? xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="@drawable/detailsback">    
  >

<TextView  
android:id="@+id/name"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginTop="30px"
android:layout_marginBottom="10px"
/>

<EditText  
android:id="@+id/ed"
android:layout_width="200px" 
android:layout_height="200px"
android:editable="false"
android:focusable="false"
android:text="ddddddddddd"
android:layout_below="@id/name"
/>

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/tableLayout"
 >

<TableRow>    

  <Button  
android:id="@+id/btn1"
android:layout_width="50px" 
android:layout_height="50px" 
android:layout_marginRight="15px"
/>

    <Button  
android:id="@+id/btn2"
android:layout_width="40px" 
android:layout_height="50px"  
android:layout_marginRight="15px"
/>

      <Button  
android:id="@+id/btn3"
android:layout_width="100px" 
android:layout_height="20px" 
android:layout_marginRight="15px"
/>
</TableRow>

<TableRow>    

   <Button  
android:id="@+id/btn4"
android:layout_width="50px" 
android:layout_height="50px"  
android:layout_marginRight="15px"
android:layout_marginTop="5px"
/>

<Button  
android:id="@+id/btn5"
android:layout_width="30px" 
android:layout_height="50px"  
android:layout_marginRight="15px"
android:layout_marginTop="5px"    
/>
  <Button  
android:id="@+id/btn6"
android:layout_width="100px" 
android:layout_height="20px"  
android:layout_marginRight="15px"
android:layout_marginTop="5px"
/>
</TableRow>
</TableLayout>

</RelativeLayout>

在我使用的代码中

  setContentView(R.layout.main);

现在我想根据屏幕分辨率修改 TableLayout 边距,问题是如何通过代码到达 TableLayout,我试过这个:

TableLayout table=(TableLayout)findViewById(R.id.tableLayout);
    LayoutParams pa=new LayoutParams();
    pa.bottomMargin=20;
    pa.rightMargin=20;

    table.setLayoutParams(pa);

但它不起作用!

4

1 回答 1

0

我刚遇到同样的问题,我通过下面的方法解决了,

尝试换行

LayoutParams pa=new LayoutParams();

FrameLayout.LayoutParams pa= new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,android.widget.TableLayout.LayoutParams.WRAP_CONTENT);
于 2013-06-19T04:16:17.107 回答