1

我正在尝试制作一个根据内容缩放大小的 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 的解决方案。见下文:

替代文字

4

2 回答 2

3

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>
于 2010-09-03T11:14:17.363 回答
0

这个问题很老,但我想指出一个替代解决方案。

我刚刚遇到了扩展列表视图和底部按钮的问题。

我最终使用了一个版本的 Android Layout Weight解决方案。

我有一个 ListView 和一个 LinearLayout(包含我的按钮)。显然,将 layout_height 设置为 0dp 的技术可以强制视图同时显示按钮和展开列表。

于 2011-05-26T20:29:59.453 回答