我想在對話框的底部顯示一個消息和一些按鈕的對話框。如何將對話框包裝到其內容?
我打算將更多控件添加到對話框中,以便使用自定義視圖。
下面是代碼:
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檢查出來,可能會有所幫助。 (有相當多的對話樣本)(對於你「自定義對話框」或「片段對話框」應該完成工作,我想「片段對話框」會在你的情況下更好) –