RelativeLayout в диалоге имеет дополнительное пространство - PullRequest
0 голосов
/ 03 сентября 2010

Я пытаюсь создать AlertDialog, который масштабируется по размеру до содержимого.Текстовое содержимое может быть больше, чем диалоговое окно, поэтому оно должно иметь возможность прокрутки.Если я использую RelativeLayout, то все рендерится правильно, независимо от того, сколько там информации, но когда недостаточно текста для заполнения TextView, остается много дополнительного пространства.Это код:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
>
<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
  android:textColor="#fff"
  android:layout_alignParentBottom="true"
    >
</CheckBox>
<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_above="@id/saleListingCheckbox"
    android:layout_alignParentTop="true"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>
</RelativeLayout>

код Java:

@Override
protected boolean onTap(int index) {
    if (overlays.isEmpty()) {
        return false;
    }
    final SaleOverlayPushPin saleItem = overlays.get(index);

    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    LayoutInflater inflater = context.getLayoutInflater();
    dialogView = inflater.inflate(R.layout.sale_listing, null);

    textView = (TextView) dialogView.findViewById(R.id.saleListingTextView);

    checkbox = (CheckBox) dialogView.findViewById(R.id.saleListingCheckbox);
    checkbox.setOnCheckedChangeListener(this);

    alertDialog.setView(dialogView);

    alertDialog.setTitle(saleItem.getTitle());
    textView.setText(saleItem.getSnippet());
    checkbox.setTag(saleItem);
    checkbox.setChecked(saleItem.isSelected());

    alertDialog.show();

    return true;
}

А вот как это выглядит с небольшими данными и большим количеством данных:

alt textalt text

Мне удалось заставить его работать с помощью LinearLayout, но тогда у меня возникла другая проблема, когда, если текстовое содержимое больше диалогового окна, оно снимает флажок.Вот код и снимки экрана:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
    android:orientation="vertical"
>
<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_gravity="top"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>
<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
  android:textColor="#fff"
  android:layout_weight="1"
  android:layout_gravity="bottom"
    >
</CheckBox>
</LinearLayout>

alt textalt text

Я бы предпочел, чтобы поведение «маленьких данных» работало так же, как LinearLayout и «много данных»работать как RelativeLayout.Возможно ли это?

ОБНОВЛЕНИЕ: решение Барта для LinearLayout работает отлично.Удаление layout_weight из CheckBox было ключевым.

Однако RelativeLayout по-прежнему не работает должным образом.Это делает CheckBox недоступным для просмотра, если данные в TextView достаточно велики.Я все еще хотел бы знать решение для RelativeLayout, если это вообще возможно.Смотрите ниже:

alt text

Ответы [ 2 ]

2 голосов
/ 03 сентября 2010

Рабочий макет для RelativeLayout (обычно вы не должны использовать alignParentTop и alignParentBottom одновременно, если вам не нужно дополнительное пространство):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
>

<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"

    android:layout_alignParentTop="true"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>

<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
    android:textColor="#fff"
    android:layout_below="@id/saleListingLinear"
    >
</CheckBox>
</RelativeLayout>

Рабочий макет для LinearLayout (уберите вес из флажка):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="12dip"
    android:paddingRight="12dip"
    android:paddingBottom="2dip"
    android:orientation="vertical"
>
<ScrollView 
    android:id="@+id/saleListingLinear"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_gravity="top"
    >
    <TextView
        android:id="@+id/saleListingTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:textColor="#fff"
       android:textSize="7pt"
       android:paddingBottom="4dip"
    />
</ScrollView>
<CheckBox 
    android:id="@+id/saleListingCheckbox" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="Save sale"
  android:textColor="#fff"
  android:layout_gravity="bottom"
    >
</CheckBox>
</LinearLayout>
0 голосов
/ 27 мая 2011

Эта проблема старая, но я хотел бы указать на альтернативное решение.

У меня только что была эта проблема с расширенным представлением списка и кнопками внизу.

Я запустил версию решения Вес макета Android .

У меня был ListView и LinearLayout (содержащий мои кнопки).Очевидно, метод установки layout_height в 0dp работает, чтобы заставить представление отображать и кнопки, и расширяющийся список.

...