运行时动态布局变化
对于应用程序,我需要将一些对象放置在我想要放置它们的确切位置,因此这次我需要使用 AbsoluteLayout。
我想动态添加按钮,就像下面的 XML 一样 - 但在运行时。
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/backbutton"
android:text="TEST123"
android:layout_x="120px"
android:layout_y="120px"
android:layout_width="100px"
android:layout_height="100px" />
</AbsoluteLayout>
我怎样才能存档这个?我尝试使用以下代码添加按钮,但到目前为止我还没有找到设置layout_x和layout_y的函数。我该怎么做?谢谢。
AbsoluteLayout al = new AbsoluteLayout(this);
Button t = new Button(this);
t.setHeight(300);
t.setWidth(300);
t.setText("TEST123");
// x y ???
setContentView(al);
for an application I need to place some objects at the exact position that I want to have them and therefore I need to use AbsoluteLayout this time.
I want to add buttons dynamically exactly like in the following XML - but during runtime.
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/backbutton"
android:text="TEST123"
android:layout_x="120px"
android:layout_y="120px"
android:layout_width="100px"
android:layout_height="100px" />
</AbsoluteLayout>
How can I archive this? I tried it with the following code to add a button, but I haven't found a function so far to set layout_x and layout_y. How can I do this? Thanks.
AbsoluteLayout al = new AbsoluteLayout(this);
Button t = new Button(this);
t.setHeight(300);
t.setWidth(300);
t.setText("TEST123");
// x y ???
setContentView(al);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将视图添加到布局时,可以提供 LayoutParams,您可以在其中指定视图的布局信息。
然而,AbsoluteLayouts 已被弃用,这是有充分理由的。您将如何处理不同的屏幕尺寸和分辨率?您应该能够使用未弃用的布局对象的组合来创建您正在寻找的布局。请参阅开发人员文档作为起点。
如果您更详细地描述您的目标,那么有人应该能够帮助您选择合适的布局。
When you add the view to the layout, you can provide LayoutParams, which is where you specify layout information for the view.
However, AbsoluteLayouts are deprecated, with good reason. How are you going to handle different screen sizes and resolutions? You should be able to create the layout you are looking for with a combination of the non-deprecated layout objects. See developer docs as a starting point.
If you describe your goals in more detail, someone should be able to help you select the proper layout.
您应该尝试使用 Canvas 方法。
使用画布 - 不会创建实际的按钮,但您可以使用图像。
另请查看这些链接: Android 绘图按钮到带有自定义视图的画布?
You should try with Canvas methods.
Using canvas - will not create actual buttons, but you can work with images.
Also have a look to these link : Android drawing button to canvas with custom view?