2017-04-07 56 views
0

我想在对话框的底部显示一个消息和一些按钮的对话框。如何将对话框包装到其内容?

我打算将更多控件添加到对话框中,以便使用自定义视图。

下面是代码:

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(); 

结果: enter image description here

问题:

  • etHV有一个简短的文本时,对话框占据屏幕的全部空间,而我只希望它换到内容高度。
  • etHV有很长的文字,它采取所有可用空间,它轻轻一按出屏幕

我搜索了很多解决方案,但还是无法弄清楚如何解决它。

我预期的结果:

  • 对话框对齐到屏幕的底部
  • 对话框换到它的内容高度
  • 按钮必须始终可见,甚至当我长的文本设置为etHV
  • etHV有长文本时,它只占用可见对话框的剩余空间,它们变成可滚动的,用户仍然可以看到对话框底部的按钮。

你有什么解决办法吗?

+0

你需要改变你的布局..你想在对话框中做什么? textveiw和3个按钮是否正确? – sravs

+0

即时通讯不知道是什么导致问题,但:https://github.com/WithoutCaps/DialogsCheatSheet检查出来,可能会有所帮助。 (有相当多的对话样本)(对于你“自定义对话框”或“片段对话框”应该完成工作,我想“片段对话框”会在你的情况下更好) –

回答

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

您的布局问题是当我设置长文本到'etHV',这些按钮被完全隐藏。我希望这些按钮始终显示,当我将文本设置为textView – Sakura

+0

我没有在设置大文本时遇到任何问题 – sravs

+0

您是否尝试过很长的文本? (> 300行)。是的,我仍然可以向下滚动阅读全文,但该按钮被推离屏幕。 – Sakura