Атрибут layout_marginBottom в ImageButton игнорируется, когда программно надувается в представление

У меня есть автономный ImageButton, определенный в XML со следующим:

<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/btnTourStartGame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="32dp"
    android:background="@drawable/tour_btn_start"/>

Затем эта кнопка ImageButton динамически раздувается во фрагменте следующего кода. (Примечание: ltTourScreenLast является относительным макетом.)

ImageButton startButton = (ImageButton) inflater.inflate(R.layout.button_tour_start, ltTourScreenLast, false);
startButton.setOnClickListener(this);
ltTourScreenLast.addView(startButton);

В настоящее время атрибут android:layout_marginBottom, по-видимому, игнорируется, когда эта кнопка надувается, а кнопка находится прямо внизу экрана.

Мое намерение состоит в том, чтобы кнопка сидела 32dp от нижней части экрана. Как мне этого добиться?

Я ссылался на следующий пост, но безрезультатно: Проблема с макетом с полями для кнопок (Sidenote: я попытался протестировать layout_marginLeft на своем ImageButton, и, похоже, он работает. Layout_marginBottom этого не делает.)

2 ответа

Решение

Попробуйте положить ImageView внутри LinearLayout... затем надуйте эту схему.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnTourStartGame"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="32dp"
        android:background="@drawable/tour_btn_start"/>

</LinearLayout>

Раздувать следующим образом...

View view =  inflater.inflate(R.layout.button_tour_start, ltTourScreenLast, false);
ImageButton startButton = (ImageButton)view.findViewById(R.id.btnTourStartGame);
startButton.setOnClickListener(this);
ltTourScreenLast.addView(view);

Попытайтесь дать маржу снизу в вашем Java-коде, используя параметры макета

RelativeLayout.LayoutParams params=showbutton.getLayoutParams();
params.setMargins(0,0,0,32);
showbutton.setLayoutParams(params);
Другие вопросы по тегам