第一步:设计d出窗口xml:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@drawable/btn_style_alert_dialog_background"
>
<Button
android:id="@+id/btn_take_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="20dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_pick_photo"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="从相册选择"
android:background="@drawable/btn_style_alert_dialog_button"
android:textStyle="bold"
/>
<Button
android:id="@+id/btn_cancel"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:layout_marginBottom="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消"
android:background="@drawable/btn_style_alert_dialog_cancel"
android:textColor="#ffffff"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
第二步:创建SelectPicPopupWindow类继承PopupWindow:
Java代码
import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.OnClickListener
import android.view.View.OnTouchListener
import android.view.ViewGroup.LayoutParams
import android.widget.Button
import android.widget.PopupWindow
public class SelectPicPopupWindow extends PopupWindow {
private Button btn_take_photo, btn_pick_photo, btn_cancel
private View mMenuView
public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) {
super(context)
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
mMenuView = inflater.inflate(R.layout.alert_dialog, null)
btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo)
btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo)
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel)
//取消按钮
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//销毁d出框
dismiss()
}
})
//设置按钮监听
btn_pick_photo.setOnClickListener(itemsOnClick)
btn_take_photo.setOnClickListener(itemsOnClick)
//设置SelectPicPopupWindow的View
this.setContentView(mMenuView)
//设置SelectPicPopupWindowd出窗体的宽
this.setWidth(LayoutParams.FILL_PARENT)
//设置SelectPicPopupWindowd出窗体的高
this.setHeight(LayoutParams.WRAP_CONTENT)
//设置SelectPicPopupWindowd出窗体可点击
this.setFocusable(true)
//设置SelectPicPopupWindowd出窗体动画效果
this.setAnimationStyle(R.style.AnimBottom)
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000)
//设置SelectPicPopupWindowd出窗体的背景
this.setBackgroundDrawable(dw)
//mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁d出框
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop()
int y=(int) event.getY()
if(event.getAction()==MotionEvent.ACTION_UP){
if(y<height){
dismiss()
}
}
return true
}
})
}
}
第三步:编写MainActivity类实现测试:
Java代码
import android.app.Activity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.View.OnClickListener
import android.widget.TextView
public class MainActivity extends Activity {
//自定义的d出框类
SelectPicPopupWindow menuWindow
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
TextView tv = (TextView) this.findViewById(R.id.text)
//把文字控件添加监听,点击d出自定义窗口
tv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//实例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick)
//显示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0)//设置layout在PopupWindow中显示的位置
}
})
}
//为d出窗口实现监听类
private OnClickListener itemsOnClick = new OnClickListener(){
public void onClick(View v) {
menuWindow.dismiss()
switch (v.getId()) {
case R.id.btn_take_photo:
break
case R.id.btn_pick_photo:
break
default:
break
}
}
}
}
上述的代码实现了从底部d出,也可以根据PopupWindow类设置从左下部d出。
Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
AlertDialog的位置固定,而PopupWindow的位置可以随意
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
你只建了菜单,未设置点击菜单后怎么做。如下面一个例子:
import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
public class Menu_Test extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
}
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 0, 0, R.string.app_about)
menu.add(0, 1, 1, R.string.str_exit)
return super.onCreateOptionsMenu(menu)
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item)
switch(item.getItemId())
{
case 0:
openOptionsDialog()
break
case 1:
finish()
break
}
return true
}
private void openOptionsDialog()
{
new AlertDialog.Builder(this)
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
}
)
.show()
}
}
例子建了有两个选择的菜单。
其中,onOptionsItemSelected()是选择处理,也就是你的程序所缺少的。
知乎这个用的是谷歌的v4包的drawerlayout吧,这个包默认就是新建project的时候就导入了的。用法很简单的, reference/android/support/v4/widget/DrawerLayout.html 这里面就有示例,也可以百度一下,根布局用drawerlayout,如果想要左边有drawer那么就在里面再写个子布局然后layout_gravity属性写成start,想在右边就是end就这样简单欢迎分享,转载请注明来源:内存溢出
评论列表(0条)