2011-10-24 46 views

回答

0

对于您可以使用AbsoluteLayout它可以帮助您在由视图android:layout_xandroid:layout_y

更新的属性设置视图的确切位置

要添加的x,y在运行时检查这些,

Answer 1

Answer 2

+1

仅供参考,AbsoluteLayout已被弃用。 –

+0

因此,让我知道使用不赞成使用的布局会带来什么样的危害。 –

0

对于下面的代码,使用...

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

     <Button android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:id="@+id/button"/> 

</RelativeLayout> 
在此代码

只需使用量U可以设置你的按钮按您的要求添加android:layout_margin .....

0

如果您正在使用的LinearLayout:

Button button = (Button)findViewById(R.id.my_button); 
LinearLayout.LayoutParams linParams = 
    (LinearLayout.LayoutParams)button.getLayoutParams(); 
linParams.setMargins(left, top, right, bottom); 
button.setLayoutParams(linParams); 
0

创建RelativeLayout布局:

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

    <Button android:id="@+id/mybutton" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 

</RelativeLayout> 

然后在你的代码:

int posX = 10, posY = 20; 
Button b = findViewById(R.id.mybutton); 
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
layoutParams.setMargins(posX, posY, 0, 0); 
b.setLayoutParams(layoutParams); 
+0

您在xml中使用RelativeLayout并将其作为代码中的LinearLayout进行处理,是否可行? – Reno

+0

@Reno - 是的,复制并没有正确更新。固定。 – ImR

+0

@IMR此解决方案是否适用于每个屏幕? – Deepak