2011-05-09 54 views
0

我用.xml创建了我的用户界面,但是我想通过动态指定按钮的方向。 举例;Android中的动态按钮指示

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <Button 
     android:id="@+id/backbutton" 
     android:text="TEST123" 
     android:layout_x="120px" 
     android:layout_y="120px" 
     android:layout_width="100px" 
     android:layout_height="100px" /> 
    </AbsoluteLayout> 

然后就是我的活动我班通过动态类似的改变的东西。

AbsoluteLayout al = new AbsoluteLayout(this); 
    Button t = new Button(this); 
    t.setHeight(300); 
    t.setWidth(300); 
    t.setText("TEST123"); 
    // x y ??? 
    setContentView(al); 

我改变了高度和宽度..但我找不到改变x和y方向的方法。

回答

1

尝试setPadding(int top,int left,int right,int bottom)方法。

AbsoluteLayout al = new AbsoluteLayout(this); 
    Button t = new Button(this); 
    t.setHeight(300); 
    t.setWidth(300); 
    t.setText("TEST123"); 
    t.setPadding(120,120,0,0); 
    setContentView(al); 

我不知道你的屏幕的其他元素是如何装修的,但我只是认为按钮,你在你的屏幕上有唯一的元素。

+0

你能举个例子记录我的代码吗? – Dapina 2011-05-09 20:31:07

+0

顶部,左边,右边,底部是什么意思? – Dapina 2011-05-09 20:35:53

+0

它们是填充参数。 – yogsma 2011-05-09 20:38:08

0

顺便说一句你知道1 dip =多少px?

http://developer.android.com/guide/practices/screens_support.html

密度独立像素(DP) - 应用程序可以在确定其UI使用,以表达布局尺寸或位置的密度无关的方式的虚拟像素。

与密度无关的像素相当于160 dpi屏幕上的一个物理像素,平台假定的基准密度(如本文后面所述)。在运行时,平台根据所使用屏幕的实际密度,透明地处理所需dp单位的任何缩放比例。将dp单位转换为屏幕像素很简单:像素= dps *(密度/ 160)。例如,在240 dpi屏幕上,1 dp将等于1.5个物理像素。强烈建议使用dp单位来定义应用程序的用户界面,以此来确保在不同屏幕上正确显示您的用户界面。

+0

我认为接受其他答案是正确的,而不是我的可能是合适的。因为它回答了实际的原始问题。我真的可以发表评论,但我不确定我是否有足够的人物,所以我做了答案。 – FoamyGuy 2011-05-10 13:09:02