安卓:更改视图的绝对位置编程 [英] Android: Change absolute position of a view programmatically
问题描述
如果您使用的是AbsoluteLayout(我知道,这是德precated,但它是解决我的<一个唯一的方法href="http://stackoverflow.com/questions/3438656/android-scrollable-horizontal-and-vertical-imageview-with-buttons-overlayed">问题)可以给childViews Android的标签:layout_x
和安卓layout_y
来设定它们的绝对在位置 AbsoluteLayout
。
不过,我不想设置在XML这些信息,因为我只知道他们在运行时。所以,我怎么可以设置这些参数在运行时编程?我没有看到像 view.setLayoutX(INT X)
什么的查看任何方法。
下面是我的XML,它工作得很好,当我设置了 layout_x
和 layout_y
值:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; AbsoluteLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / myLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT&GT;
&LT; ImageView的
机器人:SRC =@可绘制/ myImageView
机器人:layout_width =1298px
机器人:layout_height =945px
机器人:layout_x =0px
机器人:layout_y =0px/&GT;
&LT;按钮
机器人:ID =@ + ID / myButton1
机器人:文本=23
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_x =50像素
机器人:layout_y =300像素
机器人:标签=23/&GT;
&LT;按钮
机器人:ID =@ + ID / myButton2
机器人:文本=48
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_x =50像素
机器人:layout_y =300像素
机器人:标签=48/&GT;
&LT; / AbsoluteLayout&GT;
其实,我并不想设置在XML中的任何按钮了,而是通过远程检索一些信息,并添加依赖于信息的按钮。
下面是部分code我用这样在我的 onCreateMethod
添加这些按钮:
的(MyRemoteObject的RemoteObject:名单){
Button按钮=新的按钮(这一点);
button.setOnClickListener(听众);
button.setTag(remoteObject.id);
button.setText(remoteObject.id);
// button.setLayoutX(remoteObject.x)????
// button.setLayoutY(remoteObject.y)????
myLayout.addView(按钮);
}
使用addView的,是以版本<一href="http://developer.android.com/reference/android/widget/AbsoluteLayout.LayoutParams.html">LayoutParams:
的LayoutParams PARAMS = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(按钮,则params);
If you use an AbsoluteLayout (I know that it is deprecated, but it was the only way to solve my problem) you can give the childViews the tag android:layout_x
and android:layout_y
to set their absolute position within the AbsoluteLayout
.
However I don't want to set these information in the xml, because I only know them at runtime. So how can I set these parameters at runtime programmatically? I don't see any method on the View like view.setLayoutX(int x)
or something.
Here is my XML, which works fine, when I set the layout_x
and layout_y
values:
<?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>
In fact, I don't want to set any button within the xml anymore, but rather retrieve some information via remote and add buttons depending on that information.
Here is the part the code I'm using so in my onCreateMethod
to add these buttons:
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);
}
Use the version of addView that takes LayoutParams:
LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
这篇关于安卓:更改视图的绝对位置编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!