我正在尝试制作一个根据内容缩放大小的 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>
爪哇代码:
@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;
}
这是使用少量数据和大量数据时的样子:
我已经能够使用 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>
我希望“小数据”行为像 LinearLayout 一样工作,而“大量数据”行为像 RelativeLayout 一样工作。这可能吗?
更新:bart 的 LinearLayout 解决方案完美运行。从 CheckBox 中删除 layout_weight 是关键。
但是,RelativeLayout 仍然无法按预期工作。如果 TextView 中的数据足够大,它会使 CheckBox 不可见。如果可能的话,我仍然很想知道 RelativeLayout 的解决方案。见下文: