2017-04-07 31 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

相關問題