是否可以在Android中设置视图的绝对位置?(我知道有一个AbsoluteLayout,但它被弃用了......)
例如,如果我有一个240x320px的屏幕,我怎么能添加一个ImageView20x20px,使其中心位于(100,100)?
如果你使用AbsoluteLayout(我知道它已被弃用,但这是解决我问题的唯一方法)你可以给childViews标签android:layout_x并android:layout_y设置它们的绝对位置AbsoluteLayout.
但是我不想在xml中设置这些信息,因为我只在运行时知道它们.那么如何在运行时以编程方式设置这些参数呢?我没有在View上看到任何方法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>
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) View我在将视图(我的类扩展)放置在特定位置时遇到问题。我设计了一款将屏幕视为网格的游戏。我从用户那里获取特定视图的 x 和 y 坐标(我有不同类型的视图)。
我的第一个任务是在其位置上设置正确的视图。我该怎么做?我用的RelativeLayout是屏幕。
PS我不想使用AbsoluteLayout参数,因为它被截断了。