简体   繁体   中英

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). 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. When I change the lp's LayoutParam to FILL_PARENT the button is then stretched to take up the entire screen (which makes sense).

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.

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. 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. 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 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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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