如何将对话框包装到其内容?

问题描述 投票:0回答:1

我想在对话框的底部显示一个带有消息和一些按钮的对话框。

我打算在对话框中添加更多控件,因此我使用自定义视图。

这是代码:

我的hv_popup.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:background="#ffff00">

    <LinearLayout
        android:id="@+id/hvBottomBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"> 

        <Button
            android:id="@+id/btnSwitch"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="MTD/HV" />

        <Button
            android:id="@+id/btnEDIT"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="EDIT" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CLOSE" />

    </LinearLayout>

    <TextView
        android:id="@+id/etHV"
        android:background="#000000"
        android:textColor="#ffffff"
        android:textIsSelectable="true"
        android:textSize="12sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/hvBottomBar"/>
</RelativeLayout>

Java代码:

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

dialog.setContentView(R.layout.hv_popup);

final TextView tv = (TextView) dialog.findViewById(R.id.etHV);
tv.setText("text");
dialog.show();

结果:

问题:

  • etHV有一个短文本时,对话框占据了屏幕的整个空间,而我只希望它包装到内容高度。
  • etHV有一个长文本时,它占用了所有空间,它将按钮从屏幕上移开

我的预期结果:

  • 对话框与屏幕底部对齐
  • 对话框换行到其内容高度
  • 即使我将长文本设置为etHV,按钮也必须始终可见。
  • etHV有长文本时,它只占用可见对话框的剩余空间,它可以滚动,用户仍然可以看到对话框底部的按钮。
android dialog android-relativelayout android-wrap-content
1个回答
0
投票

您需要更改布局以包装内容。尝试以下布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffff00">
    <TextView
        android:id="@+id/etHV"
        android:background="#000000"
        android:textColor="#ffffff"
        android:textIsSelectable="true"
        android:textSize="12sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:id="@+id/hvBottomBar"
        android:layout_below="@id/etHV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnSwitch"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="MTD/HV" />

        <Button
            android:id="@+id/btnEDIT"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="EDIT" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CLOSE" />

    </LinearLayout>


</RelativeLayout>
© www.soinside.com 2019 - 2024. All rights reserved.