Android自定义弹窗效果
2015-07-21 21:59
627 查看
Android的弹窗效果有很多种,就最简单而言,就可以调用一个AlertDialog弹窗显示,可是要自定义弹窗效果有以下这种方法,就我个人而言感觉挺方便的,适用性也挺广的。
首先先简单写个AlertDialog的使用
接下来就是自定义弹窗效果图:
下面是示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:id="@+id/top"
>
<TextView android:layout_width="match_parent"
android:layout_height="40.5dip"
android:text="@string/select_head"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:gravity="center"
android:textColor="#aaaaaa"
/>
<Button
android:id="@+id/takephoto"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:text="@string/takephoto"
android:background="@null"
android:textColor="#fc4643"
/>
<Button
android:id="@+id/selectfromalbum"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:text="@string/takefromalbum"
android:background="@null"
android:textColor="#fc4643"
/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:layout_marginTop="7.2dip"
android:text="@string/cancel"
android:background="@null"
android:textColor="#fc4643"
/>
</LinearLayout>
</RelativeLayout>
public class TestPopupWindow extends Activity {
private SelectPopupWindow popupWindow;
private ImageView image;
private Button btn, btn1;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
init();
}
private void init(){
image = (ImageView) findViewById(R.id.image);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(onClickListener);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn:
test();
break;
case R.id.btn1:
startActivity(new Intent(TestPopupWindow.this, TestGsonActivity.class));
break;
case R.id.takephoto:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, 100);
break;
case R.id.selectfromalbum:
Intent intent1 = new Intent(Intent.ACTION_PICK);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 200);
break;
}
}
};
public void test(){
popupWindow = new SelectPopupWindow(this, onClickListener);
popupWindow.showAtLocation(findViewById(R.id.main),
Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
}
首先先简单写个AlertDialog的使用
public void showDialog(){ AlertDialog dialog = new AlertDialog.Builder(this) .setTitle("提示") .setMessage(getResources().getString("你确定退出吗?") .setPositiveButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).create(); dialog.show(); } }
接下来就是自定义弹窗效果图:
下面是示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:id="@+id/top"
>
<TextView android:layout_width="match_parent"
android:layout_height="40.5dip"
android:text="@string/select_head"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:gravity="center"
android:textColor="#aaaaaa"
/>
<Button
android:id="@+id/takephoto"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:text="@string/takephoto"
android:background="@null"
android:textColor="#fc4643"
/>
<Button
android:id="@+id/selectfromalbum"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:text="@string/takefromalbum"
android:background="@null"
android:textColor="#fc4643"
/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:layout_marginTop="7.2dip"
android:text="@string/cancel"
android:background="@null"
android:textColor="#fc4643"
/>
</LinearLayout>
</RelativeLayout>
public class SelectPopupWindow extends PopupWindow { Button takePhoto; Button selectFromAlbum; Button cancel; View menuView; public SelectPopupWindow(Context ctx, OnClickListener onClickListener) { LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); menuView = inflater.inflate(R.layout.modify_head, null); //可以采用各种样式,自定义view takePhoto = (Button) menuView.findViewById(R.id.takephoto); takePhoto.setOnClickListener(onClickListener); selectFromAlbum = (Button) menuView.findViewById(R.id.selectfromalbum); selectFromAlbum.setOnClickListener(onClickListener); cancel = (Button) menuView.findViewById(R.id.cancel); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dismiss(); } }); setContentView(menuView); setWidth(LayoutParams.MATCH_PARENT); setHeight(LayoutParams.WRAP_CONTENT); setFocusable(true); //弹出窗体后可点击 //setAnimationStyle(R.style.AnimBottom); //设置动画 menuView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int height = menuView.findViewById(R.id.top).getTop(); Log.e("huang", " top height:"+height+" y:"+event.getY()); int y = (int) event.getY(); if(y<height){ dismiss(); } return true; } }); } }
public class TestPopupWindow extends Activity {
private SelectPopupWindow popupWindow;
private ImageView image;
private Button btn, btn1;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
init();
}
private void init(){
image = (ImageView) findViewById(R.id.image);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(onClickListener);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn:
test();
break;
case R.id.btn1:
startActivity(new Intent(TestPopupWindow.this, TestGsonActivity.class));
break;
case R.id.takephoto:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, 100);
break;
case R.id.selectfromalbum:
Intent intent1 = new Intent(Intent.ACTION_PICK);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 200);
break;
}
}
};
public void test(){
popupWindow = new SelectPopupWindow(this, onClickListener);
popupWindow.showAtLocation(findViewById(R.id.main),
Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
}
相关文章推荐
- android教程之使用popupwindow创建菜单示例
- android PopupWindow 和 Activity弹出窗口实现方式
- Android PopupWindow 点击外面取消实现代码
- android自定义popupwindow仿微信右上角弹出菜单效果
- Yii2使用Bootbox插件实现自定义弹窗
- Android PopupWindow使用实例
- Android中PopupWindow响应返回键并关闭的2种方法
- Android入门之PopupWindow用法实例解析
- android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍
- 一段android跳转到web界面的代码
- 收藏各种技术源码
- PopupWindow gridview 无法响应Item
- PopupWindow 实现 弹出后 其余部分变灰(类似dialog)
- 一个简单的PopupWindow做的选项菜单点击按钮会在按钮下弹出几个下拉控件
- PopupWindow的使用
- PopupWindow(第一篇CSDN博客,希望能坚持写下去,也希望大家多多支持)
- PopupWindow的显示位置及简单的响应事件
- popupwindow上使用spinner,能显示,但当点击spinner时出现异常
- popupwindow 弹出框 使用详情
- PopupWindow学习大全