Android:如何在程序中编程更改视图的绝对位置

23

如果您使用AbsoluteLayout(我知道它已经被弃用了,但这是解决我的问题的唯一方法),您可以给子视图添加标签android:layout_xandroid:layout_y以在AbsoluteLayout中设置它们的绝对位置。

然而,我不想在xml中设置这些信息,因为我只能在运行时了解它们。那么我该如何在程序中动态设置这些参数呢?我没有在View上看到任何像view.setLayoutX(int x)这样的方法。

这是我的XML,当我设置layout_xlayout_y值时它可以正常工作:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
     android:src="@drawable/myImageView"
     android:layout_width="1298px"
     android:layout_height="945px"
     android:layout_x="0px"
  android:layout_y="0px" />
 <Button  
  android:id="@+id/myButton1"
  android:text="23"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_x="50px"
  android:layout_y="300px"
  android:tag="23"/>


 <Button  
  android:id="@+id/myButton2"
  android:text="48"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_x="50px"
  android:layout_y="300px"
  android:tag="48"/>

</AbsoluteLayout>

事实上,我不再想在xml中设置任何按钮,而是通过远程获取一些信息,并根据这些信息添加按钮。

下面是我正在使用的代码部分,在onCreateMethod中添加这些按钮:

  for (MyRemoteObject remoteObject: list)  {
   Button button = new Button(this);
   button.setOnClickListener (listener);
   button.setTag(remoteObject.id);
   button.setText(remoteObject.id);
   // button.setLayoutX(remoteObject.x) ????
   // button.setLayoutY(remoteObject.y) ????
   myLayout.addView(button);
  }
5个回答

16

请使用接受LayoutParams的addView版本:

LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);

1
谢谢,但是如果mLayout的类型是AbsoluteLayout,我该如何创建LayoutParams呢?AbsoluteLayout只有一个名为absolutLayout.generatelayoutParams(AttributeSet)的方法。我不知道如何首先创建一个attributeSet,以便将该集合传递给generateLayoutParams()方法。 - Pascal Klein
我使用RelativeLayout而不是Absolute,遇到了同样的问题。 - Amplify91
params.x and params.y seems like they are deprecated for RelativeLayout - ThunderWiring

10

回应你在 Mayra 的回答下的评论,我曾经使用 RelativeLayout 而不是 AbsoluteLayout 时遇到类似的问题。解决方法是使用类似的方法并将其强制转换为你的布局类型。像这样:

LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams();
params.height = 360;
params.width = 360;
params.addRule(CENTER_IN_PARENT);
this.addView(board, params);

我花了很长时间才弄清楚这个问题,所以我想把我的解决方法发布在这里,以便其他人需要时可以参考。


5

我曾经遇到过同样的问题,但找到了一个略有不同的解决方案。

int width = 100, height = 50, x = 10, y = 20;
Button button = new Button(this);
AbsoluteLayout myLayout = (AbsoluteLayout)findViewById(R.id.myLayout);
myLayout.add(button, new AbsoluteLayout.LayoutParams(width, height, x, y));

如果你想知道如何使用 "fill_parent"(提示:尝试使用-1),那么你可以将这些常量用于宽度和高度。


0

以上的答案都是正确的,但这个会更完美。

AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,DragLayer.int left_location,int top_location);
 layout.addView(view,layoutParams)

0

我不知道为什么,但是当移动顶部和左侧边缘时,Android 会保持右侧和底部边缘在同一位置。由于我无法正确更改宽度和高度(图像会消失),所以我使用了this

LayoutParams layoutParams=new LayoutParams(width, height);
layoutParams.leftMargin = newLeft;
layoutParams.topMargin = newTop;
imageView.setLayoutParams(layoutParams);

我不知道这是否是最好的方法,但对我有效。我已在2.2+中进行了测试,效果很好!

您必须导入android.widget.LinearLayout.LayoutParams;因为默认值是ViewGroup的LayoutParams(by Davit T


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接