小編給大家分享一下Android如何實現(xiàn)自定義Dialog的大小,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體如下:
Android應用開發(fā)中,無論是出于功能還是增加用戶體驗,彈出對話框(Dialog)進行一些操作提示是非常必要的。Android系統(tǒng)有自帶的各種樣式的對話框,但是根據(jù)項目需要可能從效果上滿足不了需求,只時我們就要自定義對話框。
我們可以自定義Dialog的樣式及展示布局,做出我們想要的對話框,但有的時候,我們做出的對話框要么顯示太大,要么顯得太小,或者是在不同的頁面大小不一樣,需要做個統(tǒng)一!此時我們就需要對Dialog大小進行控制,今天就簡單地講下這個。貼出代碼,注釋中有詳細說明。
先是我們自定義Dialog的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="141dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="92dp" android:background="@drawable/dialogbackground" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/dialog_tips_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="10dp" android:lineSpacingExtra="3dp" android:lineSpacingMultiplier="1.2" android:textColor="#333333" android:textSize="15sp" android:visibility="gone" /> <TextView android:id="@+id/dialog_content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:lineSpacingExtra="3dp" android:lineSpacingMultiplier="1.2" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:text="@string/my_bank_delete_dialog"<!--這里是提示文字,可以在代碼中更改--> android:layout_marginTop="3dp" android:textColor="#333333" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/fail" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/dialogsuccess_bg" android:gravity="center" android:text="取消" android:textColor="#007aff" android:textSize="17sp" /> <Button android:id="@+id/success" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/dialogerror" android:gravity="center" android:text="確定" android:textColor="#007aff" android:textSize="17sp" /> </LinearLayout> </LinearLayout>
下面就是對話框的實現(xiàn)代碼:
首先在所在的類中定義
private Dialog mDialog;
//下面是彈出對話框的方法,在需要彈出對話框的地方調(diào)用就可以了,當然可以去掉方法,直接寫對話框代碼也行。
protected void showIsDeleteDialog() {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.common_no_title_dialog, null);
TextView tv = (TextView) view.findViewById(R.id.dialog_content_tv);
tv.setText("您要進行如下操作嗎?");//這就是上面說到的提示文字,可以在這里做修改
Button mCancel = (Button) view.findViewById(R.id.success);
Button mSure= (Button) view.findViewById(R.id.fail);
// 取消操作
mCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
//確定操作
mSure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clearRecordRequest();
mDialog.dismiss();
}
});
mDialog = new Dialog(getActivity(), R.style.IsDelDialog);//自定義的樣式,沒有貼出代碼來
mDialog.setContentView(view);
mDialog.show();
Window dialogWindow = mDialog.getWindow();
WindowManager m = getActivity().getWindowManager();
Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高度
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對話框當前的參數(shù)值
p.height = (int) (d.getHeight() * 0.8); // 高度設置為屏幕的0.6,根據(jù)實際情況調(diào)整
p.width = (int) (d.getWidth() * 0.8); // 寬度設置為屏幕的0.65,根據(jù)實際情況調(diào)整
dialogWindow.setAttributes(p);
}以上是“Android如何實現(xiàn)自定義Dialog的大小”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當前名稱:Android如何實現(xiàn)自定義Dialog的大小-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article38/deeopp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站、網(wǎng)站設計公司、動態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設、標簽優(yōu)化、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容