如果您使用 AbsoluteLayout (我知道它已被弃用,但这是解决我的问题的唯一方法 problem https://stackoverflow.com/questions/3438656/android-scrollable-horizontal-and-vertical-imageview-with-buttons-overlayed)你可以给childViews标签android:layout_x
and android:layout_y
设置它们在范围内的绝对位置AbsoluteLayout
.
但是我不想在 xml 中设置这些信息,因为我只在运行时知道它们。那么如何在运行时以编程方式设置这些参数呢?我在视图上没有看到任何方法,例如view.setLayoutX(int x)
或者其他的东西。
这是我的 XML,当我设置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>
事实上,我不想再在 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);
}
使用 addView 的版本布局参数 http://developer.android.com/reference/android/widget/AbsoluteLayout.LayoutParams.html:
LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)