如何在代码中修改(TableLayout边距嵌入在xml中的Relativelay中)

发布于 2024-10-07 17:37:01 字数 2340 浏览 0 评论 0原文

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

但它不起作用!

i have 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>

in the code i used

  setContentView(R.layout.main);

now i want to modify TableLayout margin's according to screen resolution , the problem is how to reach to TableLayout via code , i tried this :

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

    table.setLayoutParams(pa);

but it don't work !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄想挽回 2024-10-14 17:37:01

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

尝试将行更改

LayoutParams pa=new LayoutParams();

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

I just got the same problem, and I figured it out by below method,

try to change the line

LayoutParams pa=new LayoutParams();

to

FrameLayout.LayoutParams pa= new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,android.widget.TableLayout.LayoutParams.WRAP_CONTENT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文