【发布时间】: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