Кнопка не отображается в LinearLayout - PullRequest
7 голосов
/ 04 августа 2011

Я пытаюсь добавить Button в LinearLayout после TextView, но оно не отображается.

Вот мой код макета

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="45dip">

    <TextView android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:textStyle="bold"
        android:textSize="17dip"
        android:gravity="center_vertical"
        android:id="@+id/tvChild"
        android:text="Children"
        android:textColor="#ffCCCC22" />

    <Button android:id="@+id/submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Submit" />
</LinearLayout>

TextView отображается правильно с правильным текстом, но вместо Button я получаю большой пробел длиной от трех до четырех строк.

Чего мне не хватает?

Ответы [ 2 ]

6 голосов
/ 04 августа 2011

Попробуйте это.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent" android:layout_height="45dip"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TextView android:layout_width="wrap_content"
            android:layout_height="45dip" android:paddingLeft="5dip"
            android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
            android:gravity="center_vertical" android:id="@+id/tvChild"
            android:text="Children" android:textColor="#ffCCCC22"
            />
            <Button android:id="@+id/submit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:text="Submit" />
    </LinearLayout>

Используйте android:layout_width="wrap_content" вместо android:layout_width="fill_parent" для TextView и Button

6 голосов
/ 04 августа 2011

Проблема была в том, что вы устанавливали android:layout_width="fill_parent" для TextView, поэтому для отображения TextView потребовалась полная ширина экрана.И он не смог отобразить Button.

. Вот два варианта для вас:

  1. Добавление TextView и Button в одну строку.

    Измените атрибут layout_width TextView на wrap_content.

  2. Добавление TextView и Button по вертикали.

    Измените атрибут ориентации LinearLayout на vertical.

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