1.编写布局文件qr_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/qralertImageView1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/qralertTextView1"
android:text="http://127.0.0.1:8080"/>
</LinearLayout>
2.MainActivity.class里写入代码
//获取布局文件
View v= getLayoutInflater().inflate(R.layout.qr_alert, null);
ImageView i = v.findViewById(R.id.qralertImageView1);
i.setBackgroundResource(R.drawable.ic_launcher);
((TextView)v.findViewById(R.id.qralertTextView1)).setText(url);
Dialog dialog = new Dialog(this);
//去掉标题
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(v);
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
//获取屏幕60%的宽
int w= (int)((getWindowManager().getDefaultDisplay().getWidth())*0.6);
//获取屏幕35%的高
int h= (int)((getWindowManager().getDefaultDisplay().getHeight())*0.35);
params.width = w;//设置宽
params.height = h;//设置高
dialog.getWindow().setAttributes(params);
dialog.show();
网友评论