Как изменить (отступы TableLayout встроены в Relativelay в XML) в коде

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);

но это не работает!!

  • 0
    Зачем нужно добавлять эти поля из любопытства? почему бы не установить их в макете XML?
  • 0
    потому что я хочу изменить их с изменением ширины и высоты экрана с кариозными устройствами
Теги:
relativelayout
tablelayout

1 ответ

0

У меня была такая же проблема, и я понял ее ниже,

попытайтесь изменить строку

LayoutParams pa=new LayoutParams();

к

FrameLayout.LayoutParams pa= new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,android.widget.TableLayout.LayoutParams.WRAP_CONTENT);

Ещё вопросы

Сообщество Overcoder
Наверх
Меню