Android PopupWindows
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android PopupWindows,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4925字,纯文字阅读大概需要8分钟。
内容图文
![Android PopupWindows](/upload/InfoBanner/zyjiaocheng/1106/9ac25511ed1f4d1194135a967c722701.jpg)
今天了解到PopupWindows这个布局,PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。
下面是一个实例
xml文件
<LinearLayout
android:id="@+id/ll_popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff495a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal" >
<Button
android:id="@+id/item_popupwindows_camera"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/bt_nobgd"
android:textColor="#585858"
android:textSize="18sp"
android:text="拍照" />
</LinearLayout>
<TextView
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#f2f2f2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal" >
<Button
android:id="@+id/item_popupwindows_Photo"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/bt_nobgd"
android:textColor="#585858"
android:textSize="18sp"
android:text="从相册中选取" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#f3f3f3" />
<Button
android:id="@+id/item_popupwindows_cancel"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="@drawable/bt_nobgd"
android:textColor="#585858"
android:textSize="18sp"
android:text="取消" />
</LinearLayout>
MainActivity文件 用来实现拍照获取本地图库的功能。
package cn.pb.activity;
import java.io.File;
import com.example.pb_android.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private TextView t_send,t_ragion;
private Button btn_images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Init();
}
public void Init(){
t_send = (TextView) findViewById(R.id.t_send);
btn_images = (Button) findViewById(R.id.btn_images);
t_ragion = (TextView) findViewById(R.id.t_ragion);
t_send.setOnClickListener(this);
btn_images.setOnClickListener(this);
t_ragion.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.t_send:
break;
case R.id.btn_images:
new PopupWindows(MainActivity.this, v);
break;
case R.id.t_ragion:
break;
default:
break;
}
}
public class PopupWindows extends PopupWindow {
public PopupWindows(Context mContext, View parent) {
View view = View
.inflate(mContext, R.layout.item_popupwindows, null);
view.startAnimation(AnimationUtils.loadAnimation(mContext,
R.anim.fade_ins));
LinearLayout ll_popup = (LinearLayout) view
.findViewById(R.id.ll_popup);
ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
R.anim.push_bottom_in_2));
setWidth(LayoutParams.FILL_PARENT);
setHeight(LayoutParams.FILL_PARENT);
setBackgroundDrawable(new BitmapDrawable());
setFocusable(true);
setOutsideTouchable(true);
setContentView(view);
showAtLocation(parent, Gravity.BOTTOM, 0, 0);
update();
Button bt1 = (Button) view
.findViewById(R.id.item_popupwindows_camera);
Button bt2 = (Button) view
.findViewById(R.id.item_popupwindows_Photo);
Button bt3 = (Button) view
.findViewById(R.id.item_popupwindows_cancel);
bt1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
// 下面这句指定调用相机拍照后的照片存储的路径
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment
.getExternalStorageDirectory(),
"imageTemp.jpg")));
startActivityForResult(intent, 2);
dismiss(); //关闭Pop窗口
}
});
bt2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
"image/*");// 调用android的图库
startActivityForResult(intent, 1);
dismiss();
}
});
bt3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dismiss();
}
});
}
}
}
原文:http://blog.csdn.net/u011521890/article/details/46347377
内容总结
以上是互联网集市为您收集整理的Android PopupWindows全部内容,希望文章能够帮你解决Android PopupWindows所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。