我想在对话框的底部显示一个消息和一些按钮的对话框。如何将对话框包装到其内容?
我打算将更多控件添加到对话框中,以便使用自定义视图。
下面是代码:
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个按钮是否正确? – sravs
即时通讯不知道是什么导致问题,但:https://github.com/WithoutCaps/DialogsCheatSheet检查出来,可能会有所帮助。 (有相当多的对话样本)(对于你“自定义对话框”或“片段对话框”应该完成工作,我想“片段对话框”会在你的情况下更好) –