Сбой TextView трудно выровнять Top и alignBottom - PullRequest
0 голосов
/ 23 сентября 2011

Text failing to align

«Солнце красное» должно быть выровнено по верху изображения, а «2 часа дня» - по низу. Почему атрибуты layout_alignTop и layout_alignBottom не работают так, как описано в документации?

  <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="9dp">
        <ui.RoundedImageView
        android:id="@+id/img_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="9dp"
        st:cornerRadius="3"
        android:background="@drawable/black_rounded3" />
        <TextView
                android:id="@+id/uname"
                android:layout_toRightOf="@id/img_user"
                android:layout_alignTop="@id/img_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textStyle="bold" />
        <TextView
                android:id="@+id/time"
                android:layout_toRightOf="@id/img_user"
                android:layout_alignBottom="@id/img_user"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#909191" />

        <LinearLayout
                android:id="@+id/layout_g"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="9dp"
                android:layout_marginLeft="9dp"
                android:layout_alignParentRight="true"
                android:clickable="true"
                android:gravity="right">
                <TextView
                        android:gravity="right"
                        android:layout_gravity="right"
                        android:id="@+id/gtext"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        style="@style/text_detail" />
        </LinearLayout>
      </RelativeLayout>

Ответы [ 2 ]

4 голосов
/ 23 сентября 2011

Почему атрибуты layout_alignTop и layout_alignBottom не работают так, как задокументировано?

-> Они делают.

Проблема в используемом вами объекте RoundedImageView. Я предполагаю, что это onMeasure не правильно и не возвращает правильное значение для того, где находится верх и низ самого себя. Без знания чего-либо еще об этом RoundedImageView, которое вы используете, я не могу на самом деле указать вам направление, чтобы исправить это.

Я изменил ваш код на это:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="9dp">
    <ImageView
    android:id="@+id/img_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="9dp"

    android:background="@drawable/black" />
    <TextView
            android:id="@+id/uname"
            android:layout_toRightOf="@id/img_user"
            android:layout_alignTop="@id/img_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:text="the sun is red"
            android:textStyle="bold" />
    <TextView
            android:id="@+id/time"
            android:layout_toRightOf="@id/img_user"
            android:text="2pm"
            android:layout_alignBottom="@id/img_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#909191" />

    <LinearLayout
            android:id="@+id/layout_g"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="9dp"
            android:layout_marginLeft="9dp"
            android:layout_alignParentRight="true"
            android:clickable="true"
            android:gravity="right">
            <TextView
                    android:gravity="right"
                    android:layout_gravity="right"
                    android:id="@+id/gtext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    />
    </LinearLayout>
  </RelativeLayout>

И это правильно выравнивает текст. Вот скриншот:

enter image description here

0 голосов
/ 17 сентября 2014

У меня была похожая проблема с настраиваемым представлением (в моем случае напрямую унаследованным от android.view.View) и RelativeLayout.

Я не переписывал метод onMeasure() и использовал холст, полученный в onDraw(), для записи в него своей графики. Графический вывод был полностью в порядке, но выравнивание других представлений в RelativeLayout не работало, как в случае, описанном здесь.

Наконец-то выяснилось, что мой конструктор неправильно вызвал конструктор суперкласса (я забыл передать AttributeSet). После того, как я изменил первую строку моего конструктора на

public MyNewView (Context context, AttributeSet attrs) {
    super(context, attrs);
    ...
}

вдруг все заработало.

Похоже, что какое-то волшебство алгоритма разметки Android уже происходит в конструкторе android.View.view задолго до вызова onMeasure().

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