What is the alternative to AbsoluteLayout in Android?

I see messages saying that FrameLayout is an alternative, and that I should use fields to position things (it strikes me as wildly counter intuitive, but fine ... if it works, I'll take it). However, I cannot get it to work, so I am looking for help.

here is 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 lp LayoutParam to FILL_PARENT, the button is stretched to cover the entire screen (which makes sense).

How can you draw it elsewhere on the screen, no matter what else is?

As always, super grateful in advance.

[EDIT] It seems my question is not entirely clear (given the answers), so ...

, , 100 100.

, . . , , SPECIFIC. ( ). :)

+3
3

, - Android. , .

, : , 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);
+4

? - , , :

<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 due to the large number of screen resolutions that need to be supported. 100 pixels on one screen can be halfway, while it can be less than a quarter across the screen on a higher dpi device.

0
source

Source: https://habr.com/ru/post/1764217/


All Articles