Почему не работает RelativeLayout.LayoutParams.addRule ()? - PullRequest
2 голосов
/ 09 мая 2011

В OnCreate () я инициализирую mRelLayout следующим образом:

mRelLayout = (RelativeLayout) findViewById(R.id.relative_layout);

Позже я пытаюсь поместить динамически созданную кнопку внизу экрана:

RelativeLayout.LayoutParams layoutParams = 
  new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
mRelLayout.addView(dynamicallyCreateButton);

Я использую следующий XML-файл макета:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relative_layout"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView android:id="@+id/tv_intro1" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentTop="true"
            android:text="@string/intro1" />

            <Button android:id="@+id/btn_1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_intro1"
            android:layout_centerHorizontal="true"
            android:text="@string/button_label" />

        <TextView android:id="@+id/tv_label1"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/btn_1"
            android:text="" />

        <TextView 
            android:id="@+id/tv_content1"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_label1"
            android:text="" />

        <TextView android:id="@+id/tv_label2"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_content1"
            android:text="" />

        <TextView android:id="@+id/tv_content2"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_label2"
            android:text="" />

    </RelativeLayout>
</ScrollView>

Проблема в том, что dynamicallyCreateButton всегда отображается вверху экрана, а не внизу, как я хочу.

Что я делаю не так?

1 Ответ

5 голосов
/ 09 мая 2011

Вы что-то упускаете:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(/*...*/);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

dynamicallyCreateButton.setLayoutParams(layoutParams); // <== missing line

mRelLayout.addView(dynamicallyCreateButton);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...