我正在尝试制作一个可以根据内容缩放的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;
}
以下是小数据和大量数据的样子:
我已经能够使用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的解决方案。见下文:
答案 0 :(得分:2)
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>
答案 1 :(得分:0)
这个问题很老,但我想指出一个替代解决方案。
我刚才有一个扩展列表视图和底部按钮的问题。
我使用了Android Layout Weight解决方案的版本。
我有一个ListView和一个LinearLayout(包含我的按钮)。显然,将layout_height设置为0dp的技术可以强制视图显示按钮和扩展列表。