Android自訂Dialog大小控制

來源:互聯網
上載者:User

標籤:android開發   對話方塊dialog   

     Android應用開發中,無論是出於功能還是增加使用者體驗,彈出對話方塊(Dialog)進行一些操作提示是非常必要的。Android系統有內建的各種樣式的對話方塊,但是根據項目需要可能從效果上滿足不了需求,只時我們就要自訂對話方塊。

     我們可以自訂Dialog的樣式及展示布局,做出我們想要的對話方塊,但有的時候,我們做出的對話方塊要麼顯示太大,要麼顯得太小,或者是在不同的頁面大小不一樣,需要做個統一!此時我們就需要對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>

下面就是對話方塊的實現代碼:

首先在所在的類中定義 private Dialog mDialog;

//下面是彈出對話方塊的方法,在需要彈出對話方塊的地方調用就可以了,當然可以去掉方法,直接寫對話方塊代碼也行。

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);
Window dialogWindow = mDialog.getWindow();
WindowManager m = getActivity().getWindowManager();
Display d = m.getDefaultDisplay(); // 擷取螢幕寬、高度
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 擷取對話方塊當前的參數值
        p.height = (int) (d.getHeight() * 0.8); // 高度設定為螢幕的0.6,根據實際情況調整
p.width = (int) (d.getWidth() * 0.8); // 寬度設定為螢幕的0.65,根據實際情況調整
dialogWindow.setAttributes(p);
mDialog.show();
}

代碼結束!

Android自訂Dialog大小控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.