自定义一个全屏的AlertDialog。

 

 

………..

final MyDialog dialog = new MyDialog(this);
        LayoutInflater inflater = getLayoutInflater();
        LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.layout, null);
        CautionTv = (TextView) layout.findViewById(R.id.ion_tv);
        ContentTv = (TextView) layout.findViewById(R.id.ntent_tv);
        LanguageTv = (TextView) layout.findViewById(R.id.guage_tv);
        OkBt = (Button) layout.findViewById(R.id._ok_bt);
        OkBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (dialog != null) {
                    dialog.cancel();
                }
            }
        });
        dialog.show();
        dialog.setCancelable(false);
        dialog.setContentView(layout);// show方法要在前面

 

 

 

 

 



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;

import com.android.launcher.R;

/**
 * 自定义弹框
 * @author xiebin
 *
 */
public class MyDialog extends AlertDialog {
    Context mContext;

    public MyDialog(Context context) {
        super(context, R.style.MyDialog); // 自定义全屏style
        this.mContext=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏,要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity=Gravity.BOTTOM;
        layoutParams.width= LayoutParams.MATCH_PARENT;
        layoutParams.height= LayoutParams.MATCH_PARENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }
}

 

 

/Launcher/res/values/styles.xml

<!-- 全屏style -->
    <style name="MyDialog" parent="@android:style/Theme.NoTitleBar.Fullscreen">
        <!-- 是否有边框 -->
        <item name="android:windowFrame">@null</item>
        <!--是否在悬浮Activity之上  -->
        <item name="android:windowIsFloating">true</item>
        <!--标题  -->
        <item name="android:windowNoTitle">true</item>
        <!--阴影  -->
        <item name="android:windowIsTranslucent">true</item><!--半透明-->
        <!-- 点外边可以消失  -->
        <item name="android:windowCloseOnTouchOutside">false</item>
    </style>

 

IT文库 » 自定义一个全屏的AlertDialog。
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址