相关疑难解决方法(0)

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

如果你使用AbsoluteLayout(我知道它已被弃用,但这是解决我问题的唯一方法)你可以给childViews标签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>
Run Code Online (Sandbox Code Playgroud)

实际上,我不想再在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);
  }
Run Code Online (Sandbox Code Playgroud)

android position runtime absolutelayout

21
推荐指数
3
解决办法
6万
查看次数

标签 统计

absolutelayout ×1

android ×1

position ×1

runtime ×1