Я пытаюсь создать 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 text](https://i.stack.imgur.com/Yilf3.png)
![alt text](https://i.stack.imgur.com/n0gBh.png)
Мне удалось заставить его работать с помощью 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 text](https://i.stack.imgur.com/0om7P.png)
![alt text](https://i.stack.imgur.com/4Ktmg.png)
Я бы предпочел, чтобы поведение «маленьких данных» работало так же, как LinearLayout и «много данных»работать как RelativeLayout.Возможно ли это?
ОБНОВЛЕНИЕ: решение Барта для LinearLayout работает отлично.Удаление layout_weight из CheckBox было ключевым.
Однако RelativeLayout по-прежнему не работает должным образом.Это делает CheckBox недоступным для просмотра, если данные в TextView достаточно велики.Я все еще хотел бы знать решение для RelativeLayout, если это вообще возможно.Смотрите ниже:
![alt text](https://i.stack.imgur.com/G6bqU.png)