【问题标题】:How to wrap dialog to its content?如何将对话框包装到其内容?
【发布时间】:2017-04-07 01:52:00
【问题描述】:

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

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

代码如下:

我的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 有长文本时,它只占用可见对话框的剩余空间,变为可滚动并且用户仍然可以看到对话框底部的按钮。

【问题讨论】:

  • 你需要改变你的布局..你想在对话框中做什么? textveiw 和 3 按钮正确吗?
  • 我不确定是什么导致了问题,但是:github.com/WithoutCaps/DialogsCheatSheet 检查一下,可能会有所帮助。 (有很多对话框示例)(对于您来说,“自定义对话框”或“片段对话框”应该可以完成工作,我猜“片段对话框”在您的情况下会更好)

标签: android dialog android-relativelayout android-wrap-content


【解决方案1】:

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

<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>

【讨论】:

  • 您的布局问题是当我将长文本设置为etHV 时,按钮被完全隐藏。当我将文本设置为 textView 时,我希望这些按钮始终显示
  • 我在设置大文本时没有遇到任何问题
  • 你试过很长的文字吗? (> 300 行)。是的,我仍然可以向上滚动以阅读全文,但按钮被推开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多