代码之家  ›  专栏  ›  技术社区  ›  Pascal Klein

android:以编程方式更改视图的绝对位置

  •  21
  • Pascal Klein  · 技术社区  · 16 年前

    如果你使用绝对布局(我知道它被否决了,但它是解决我的 problem )你可以给孩子看标签 android:layout_x 和 android:layout_y 将其绝对位置设置在 AbsoluteLayout .

    但是,我不想在XML中设置这些信息,因为我只在运行时知道它们。那么,如何在运行时以编程方式设置这些参数呢?我在视图上看不到任何方法 view.setLayoutX(int x) 或者什么的。

    这是我的XML,当我设置 layout_x 和 layout_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 回复  |  直到 9 年前
        1
  •  14
  •   Cheryl Simon    16 年前

    使用需要 LayoutParams :

    LayoutParams params = mLayout.generateLayoutParams();
    params.x = remoteObject.x;
    params.y = remoteObject.y;
    mLayout.addView(button, params);
    
        2
  •  10
  •   Amplify91    15 年前

    为了回应你对迈拉答案的评论,我有一个类似的问题,即相对布局而不是绝对布局。解决方案是使用类似的方法并将其转换为布局类型。像这样:

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

    我花了很长时间才弄明白,所以我想在别人需要的时候把它贴在这里。

        3
  •  5
  •   Shaheen Ghiassy    9 年前

    我也遇到了同样的问题,但找到了一个不同的解决方案。

    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”的用法(提示try-1),那么你可以使用这些常量来表示宽度和高度。

        4
  •  0
  •   Community Mohan Dere    9 年前

    我不知道为什么,但是当移动顶部和左侧边缘时,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 )

        5
  •  0
  •   Asthme    12 年前

    以上答案是对的,但这个答案会更完美。

    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)