Android:以编程方式更改视图的绝对位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3441475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Change absolute position of a view programmatically
提问by Pascal Klein
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
.
如果您使用 AbsoluteLayout(我知道它已被弃用,但这是解决我的问题的唯一方法),您可以为 childViews 提供标签android:layout_x
并android:layout_y
在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.
但是我不想在 xml 中设置这些信息,因为我只在运行时知道它们。那么如何在运行时以编程方式设置这些参数呢?我在 View 上看不到任何方法之类的view.setLayoutX(int x)
东西。
Here is my XML, which works fine, when I set the layout_x
and layout_y
values:
这是我的 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>
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.
事实上,我不想再在 xml 中设置任何按钮,而是通过远程检索一些信息并根据该信息添加按钮。
Here is the part the code I'm using so in my onCreateMethod
to add these buttons:
这是我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);
}
采纳答案by Cheryl Simon
Use the version of addView that takes LayoutParams:
使用采用LayoutParams的 addView 版本:
LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
回答by Amplify91
In response to your comment on Mayra's answer, I had a similar issue with RelativeLayout instead of AbsoluteLayout. The solution was to use a similar method and cast it as your layout type. Something like this:
为了回应您对 Mayra 回答的评论,我在使用 RelativeLayout 而不是 AbsoluteLayout 时遇到了类似的问题。解决方案是使用类似的方法并将其转换为您的布局类型。像这样的东西:
LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams();
params.height = 360;
params.width = 360;
params.addRule(CENTER_IN_PARENT);
this.addView(board, params);
It took me forever to figure this out so I wanted to post it here for someone else if they needed it.
我花了很长时间才弄明白这一点,所以如果他们需要的话,我想把它贴在这里给其他人。
回答by Lauri
I had just the same problem but found a somewhat different solution.
我遇到了同样的问题,但找到了一个有点不同的解决方案。
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));
And if you find out what to use for "fill_parent" (hint try -1) then you may use those constants for width and height.
如果您发现“fill_parent”要使用什么(提示尝试-1),那么您可以使用这些常量来表示宽度和高度。
回答by Rafael Brasil
I don't know why, but when moving top and left edges the Android keeps the right and bottom edges in same place. As I could not change the width and height properly (the image was disappearing), I've used this:
我不知道为什么,但是当移动顶部和左侧边缘时,Android 会将右侧和底部边缘保持在同一位置。由于我无法正确更改宽度和高度(图像正在消失),我使用了这个:
LayoutParams layoutParams=new LayoutParams(width, height);
layoutParams.leftMargin = newLeft;
layoutParams.topMargin = newTop;
imageView.setLayoutParams(layoutParams);
I don't know if it's the best way, but worked for me. I've tested it in 2.2+ and works fine!
我不知道这是否是最好的方法,但对我有用。我已经在 2.2+ 中测试过它并且工作正常!
You must import android.widget.LinearLayout.LayoutParams; because the default is ViewGroup's LayoutParams (by Davit T)
你必须导入 android.widget.LinearLayout.LayoutParams; 因为默认是 ViewGroup 的 LayoutParams(由Davit T 提供)
回答by Asthme
the above answers are right.but this one would be more perfect
上面的答案是对的。但是这个会更完美
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)