简体   繁体   English

Android中AbsoluteLayout的替代方案是什么?

[英]What is the alternative to AbsoluteLayout in Android?

I see posts saying that FrameLayout is the alternative, and that I should use margins to position things (this strikes me as wildly counter intuitive, but ok... if it works, I'll take it). 我看到帖子说FrameLayout是另一种选择,我应该使用边距来定位事物(这对我来说非常直观,但是好吧......如果它有效,我会接受它)。 However, I can't get it to work, so, I'm looking for assistance. 但是,我无法让它工作,所以,我正在寻求帮助。

here's my code 这是我的代码

    FrameLayout layout = new FrameLayout(this);
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    Button btn = new Button(this);
    btn.setBackgroundResource(R.drawable.btn);

    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.setMargins(100,100,100,100);

    btn.setLayoutParams(lp);   //i've tried with and without this line, no change

    layout.addView(btn , lp);

The button is drawn at 0,0 no matter what I do. 无论我做什么,按钮都是0,0。 When I change the lp's LayoutParam to FILL_PARENT the button is then stretched to take up the entire screen (which makes sense). 当我将lp的LayoutParam更改为FILL_PARENT时,按钮会被拉伸以占据整个屏幕(这很有意义)。

HOW do you get it to draw somewhere else on the screen, irrespective of what else is there? 你如何在屏幕上的其他地方画画,不管其他地方是什么?

As always, super grateful in advance. 一如既往,超级感激提前。

[EDIT] It seems my question isn't entirely clear (given the answers) so... [编辑]看来我的问题并不完全清楚(给出答案)所以......

In the code above, the intent is to create a button, pass it to a layout and have it draw at 100,100 on the screen. 在上面的代码中,目的是创建一个按钮,将其传递给布局并让它在屏幕上以100,100绘制。

I'm aware of the fact that this may mean different things on different devices. 我知道这可能意味着不同设备上有不同的东西。 I'm ok with that. 我很好。 I simply need a way to, programatically, and at run time, place an item at a SPECIFIC location. 我只需要一种方法,以编程方式,并在运行时,将项目放在SPECIFIC位置。 I don't want to rely on gravity (or the laws of thermodynamics). 我不想依靠引力(或热力学定律)。 I just want to specify a location and have the element appear there :) 我只想指定一个位置并让元素出现在那里:)

As many have pointed out, setting a button at an absolute pixel position on the screen is a really bad idea, and will never work across all of the available android phones. 正如许多人所指出的那样,在屏幕上的绝对像素位置设置按钮是一个非常糟糕的主意,并且永远不会在所有可用的Android手机上工作。 I'm sure there is a better way to achieve the layout you want. 我确信有更好的方法来实现您想要的布局。

However, to answer the question as asked: to position it at runtime you can use the AbsoluteLayout.LayoutParms. 但是,要回答问题:在运行时定位它可以使用AbsoluteLayout.LayoutParms。

AbsoluteLayout absoluteLayout = //get absolute layout

Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;

absoluteLayout.addView(button, params);

A good explanation of the different available layouts is available at http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts 有关不同可用布局的详细说明,请访问http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts

As I do not completely understand from your description what you're trying to accomplish (do you want to overlay stuff?) maybe the visual examples there can help you in understanding how the layouts work and how to position stuff on them. 因为我没有从你的描述中完全理解你想要完成什么(你想覆盖一些东西吗?)也许那里的视觉例子可以帮助你理解布局如何工作以及如何在它们上面定位东西。

What exactly is your final objective with the layout? 布局的最终​​目标究竟是什么? If all you need is a button -- say, in the center of the screen, you can just make a layout with: 如果你需要的只是一个按钮 - 比如说,在屏幕的中央,你可以制作一个布局:

<RelativeLayout
    android:xmlns="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >
    <Button
        android:id="@+id/the_button"
        android:layout_width="wrap_content"
        android:layout_height = "wrap_content"
        android:text="@string/the_button_text"
        />
</RelativeLayout>

AbsoluteLayout is a bad idea because of the large number of screen resolutions you need to support. AbsoluteLayout是一个坏主意,因为您需要支持大量的屏幕分辨率。 100 pixels over on one screen may be halfway, while it may be less than a quarter across the screen on a higher dpi device. 一个屏幕上的100个像素可能是中途,而在更高的dpi设备上,屏幕上可能不到四分之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM