1.dailog的使用(全屏使用)
package com.example.zhilongzhang.allme.dialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.zhilongzhang.allme.R;
public class PublicDialog extends Dialog {
Context mContext;
btnInterface listener;
TextView tvMseeage;
String message="提示语";
public interface btnInterface {
void btnConfirm();
}
public PublicDialog(Context context,String message,btnInterface listener) {
super(context, R.style.Dialog_public);
this.mContext = context;
this.listener = listener;
this.message = message;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_style1);
//设置点击其他地方弹窗是否消失
this.setCanceledOnTouchOutside(true);
fullWidth();
initData();
}
//让弹窗的宽度充满屏幕
private void fullWidth() {
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.gravity = Gravity.BOTTOM; //底部
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
}
private void initData(){
findViewById(R.id.bt_confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.btnConfirm();
}
});
findViewById(R.id.bt_cancle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
tvMseeage = findViewById(R.id.tv_message);
tvMseeage.setText(message);
}
}
Style的样式
<style name="Dialog_public">
<item name="android:windowBackground">@color/noColor</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
页面布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#60f5f598">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_centerInParent="true"
android:layout_margin="20dp">
<TextView
android:id="@+id/tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="提示语"
android:gravity="center"
android:layout_margin="20dp"/>
<Button
android:id="@+id/bt_cancle"
android:layout_width="60dp"
android:layout_height="40dp"
android:text="取消"
android:layout_below="@+id/tv_message"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/bt_confirm"
android:layout_width="60dp"
android:layout_height="40dp"
android:text="确定"
android:layout_below="@+id/tv_message"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"/>
</RelativeLayout>
</RelativeLayout>
2.PopupWindow的使用
package com.example.zhilongzhang.allme.dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import com.example.zhilongzhang.allme.R;
public class MePopup extends PopupWindow implements View.OnClickListener {
private Context mContext;
private OnItemOnClickListener mItemOnClickListener;
public interface OnItemOnClickListener {
void onItemClick(int position);
void onClencl();
}
public MePopup(Context context) {
this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
public MePopup(Context context, int width, int height) {
this.mContext = context;
setFocusable(true);
setTouchable(true);
setOutsideTouchable(true);
setWidth(width);
setHeight(height);
setBackgroundDrawable(new ColorDrawable());
setContentView(LayoutInflater.from(mContext).inflate(R.layout.view_popup, null));
initUI();
//设置动画所对应的style
// setAnimationStyle(R.style.popAniStyle);
}
int zongWidth=200;
private void initUI() {
getContentView().findViewById(R.id.zong_popup).post(new Runnable() {
@Override
public void run() {
zongWidth=getContentView().findViewById(R.id.zong_popup).getWidth();
Log.e("das","zong="+zongWidth);
}
});
}
public void setItemOnClickListener(OnItemOnClickListener onItemOnClickListener) {
this.mItemOnClickListener = onItemOnClickListener;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.popup1:
mItemOnClickListener.onItemClick(0);
break;
case R.id.popup2:
mItemOnClickListener.onClencl();
break;
}
}
public void show(int position, final View v, final int width) {
switch (1) {
//在指定view的正左下方,无偏移
case 0:
showAsDropDown(v);
break;
//相对指定view正左下方的位置,有偏移,第一个参数是横向X,第二个是纵向Y
case 1:
//zongWidth代表弹窗的宽度表示弹窗显示在右边
showAsDropDown(v, width -zongWidth, 0);
break;
}
}
}
布局
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/zong_popup"
android:background="#f0fa7c">
<TextView
android:id="@+id/popup1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="第一个"/>
<View
android:layout_width="100dp"
android:layout_height="1dp"
android:background="#000000"/>
<TextView
android:id="@+id/popup2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="第二个"/>
</LinearLayout>
用法
MePopup popup=new MePopup(ShowDialogActivity.this);
popup.setItemOnClickListener(new MePopup.OnItemOnClickListener() {
@Override
public void onItemClick(int position) {
}
@Override
public void onClencl() {
}
});
popup.show(0,findViewById(R.id.tv4),poppWidth);
其中的show可以控制其显示的位置