当前位置:首页 » 安卓系统 » android自定义弹出窗

android自定义弹出窗

发布时间: 2022-10-31 06:27:01

Ⅰ android将activity设置成自定义的Dialog怎么调整大小

在任何时候,除非一定需要,否则不要强指你的弹出框的宽度和高度。

你把你的弹框的宽度设成wrap型的,就可以根据它的子内容的宽度自动拉伸

Ⅱ 如何打造Android自定义的下拉列表框控件

一、概述
Android中的有个原生的下拉列表控件Spinner,但是这个控件有时候不符合我们自己的要求,
比如有时候我们需要类似windows 或者web网页中常见的那种下拉列表控件,类似下图这样的:

这个时候只有自己动手写一个了。其实实现起来不算很难,
本文实现的方案是采用TextView +ImageView+PopupWindow的组合方案。
先来看看我们的自己写的控件效果图吧:(源码在文章下面最后给出哈!)

二、自定义下拉列表框控件的实现
1. 自定义控件用到的布局文件和资源:
结果框的布局页面:dropdownlist_view.xml:
<?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="match_parent"
android:orientation="horizontal"
android:id="@+id/compound"
android:background="@drawable/dropdown_bg_selector" >

<TextView
android:id="@+id/text"
android:layout_width="250dp"
android:layout_height="40dp"
android:paddingLeft="10dp"
android:text="文本文字"
android:gravity="center_vertical"
android:textSize="14sp"
android:padding="5dp"
android:singleLine="true" />
<ImageView
android:id="@+id/btn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_toRightOf="@+id/text"
android:src="@drawable/dropdown"
android:padding="5dp"
android:layout_centerVertical="true"
android:gravity="center"/>
</RelativeLayout>

下拉弹窗列表布局页面:dropdownlist_popupwindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:divider="#666666"
android:dividerHeight="1dp"
></ListView>

</LinearLayout>

selector资源文件:
dropdown_list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dropdownlist_item_press"/>
<item android:drawable="@color/dropdownlist_item"/>
</selector>

dropdown_bg_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dropdownlist_press"/>
<item android:drawable="@color/dropdownlist_bg"/>
</selector>

2. 自定义下拉列表框控件类的实现:
我们采用了TextView+ImageView+PopupWindow的组合方案,所以我的自定义控件需要重写ViewGroup,由于我们已经知道了,布局方向为竖直方向,所以这里,
我直接继承LinearLayout来写这个控件。具体实现代码如下:
package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

@SuppressLint("NewApi")
/**
* 下拉列表框控件
* @author caiming
*
*/
public class XCDropDownListView extends LinearLayout{

private TextView editText;
private ImageView imageView;
private PopupWindow popupWindow = null;
private ArrayList<String> dataList = new ArrayList<String>();
private View mView;
public XCDropDownListView(Context context) {
this(context,null);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs) {
this(context, attrs,0);
// TODO Auto-generated constructor stub
}
public XCDropDownListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
initView();
}

public void initView(){
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View view = layoutInflater.inflate(R.layout.dropdownlist_view, this,true);
editText= (TextView)findViewById(R.id.text);
imageView = (ImageView)findViewById(R.id.btn);
this.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(popupWindow == null ){
showPopWindow();
}else{
closePopWindow();
}
}
});
}
/**
* 打开下拉列表弹窗
*/
private void showPopWindow() {
// 加载popupWindow的布局文件
String infServie = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) getContext().getSystemService(infServie);
View contentView = layoutInflater.inflate(R.layout.dropdownlist_popupwindow, null,false);
ListView listView = (ListView)contentView.findViewById(R.id.listView);

listView.setAdapter(new XCDropDownListAdapter(getContext(), dataList));
popupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transparent));
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(this);
}
/**
* 关闭下拉列表弹窗
*/
private void closePopWindow(){
popupWindow.dismiss();
popupWindow = null;
}
/**
* 设置数据
* @param list
*/
public void setItemsData(ArrayList<String> list){
dataList = list;
editText.setText(list.get(0).toString());
}
/**
* 数据适配器
* @author caiming
*
*/
class XCDropDownListAdapter extends BaseAdapter{

Context mContext;
ArrayList<String> mData;
LayoutInflater inflater;
public XCDropDownListAdapter(Context ctx,ArrayList<String> data){
mContext = ctx;
mData = data;
inflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// 自定义视图
ListItemView listItemView = null;
if (convertView == null) {
// 获取list_item布局文件的视图
convertView = inflater.inflate(R.layout.dropdown_list_item, null);

listItemView = new ListItemView();
// 获取控件对象
listItemView.tv = (TextView) convertView
.findViewById(R.id.tv);

listItemView.layout = (LinearLayout) convertView.findViewById(R.id.layout_container);
// 设置控件集到convertView
convertView.setTag(listItemView);
} else {
listItemView = (ListItemView) convertView.getTag();
}

// 设置数据
listItemView.tv.setText(mData.get(position).toString());
final String text = mData.get(position).toString();
listItemView.layout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(text);
closePopWindow();
}
});
return convertView;
}

}
private static class ListItemView{
TextView tv;
LinearLayout layout;
}

}

三、如何使用该自定义下拉列表框控件
使用该控件和使用普通的自带的控件一样,首先需要在布局文件中引用该控件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.czm.xcdropdownlistview.MainActivity"
tools:ignore="MergeRootFrame" >

<com.czm.xcdropdownlistview.XCDropDownListView
android:id="@+id/drop_down_list_view"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />

</RelativeLayout>

其次,就是在代码中使用该控件:
package com.czm.xcdropdownlistview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
/**
* 使用下拉列表框控件 示例
* @author caiming
*
*/
public class MainActivity extends Activity {

XCDropDownListView dropDownListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dropDownListView = (XCDropDownListView)findViewById(R.id.drop_down_list_view);
ArrayList<String> list = new ArrayList<String>();
for(int i = 0;i< 6;i++){
list.add("下拉列表项"+(i+1));
}
dropDownListView.setItemsData(list);

}

}

对了,这个控件中,我没有实现点击item项回调接口,这个可能对有些写惯了回调的可能觉得少了写什么的感觉,有兴趣的你可以自己添加相关回调操作哈,这个大家应该都会把。

Ⅲ android如何写一个自定义的dialog可以在Title的位置弹出来

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/title_height"
android:background="@drawable/bg_top_title"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="20sp"
android:layout_centerInParent="true"/>


<ImageView
android:layout_width="@dimen/header_btn_width"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_alignParentTop="true"
android:paddingLeft="4dp"
android:id="@+id/right_button"
android:src="@drawable/arrow_dropdown_pressed"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
弹出的dialog的Layout为
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/dropdownBckgrnd"
android:background="@drawable/bg_pop_up_dimmer"
>

<ImageView
android:layout_width="@dimen/header_btn_width"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_alignParentTop="true"
android:paddingLeft="4dp"
android:id="@+id/right_button"
android:src="@drawable/arrow_dropdown_pressed"
android:layout_alignParentRight="true"
/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="35dp"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="@drawable/topmenu_popup_down">

<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/main_menu_button_background"
android:layout_margin="@dimen/button_margin_top"
android:text="aaa"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/main_menu_button_background"
android:layout_margin="@dimen/button_margin_top"
android:text="bbb"
/>

<Button
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/main_menu_button_background"
android:layout_margin="@dimen/button_margin_top"
android:text="ccc"
/>
</LinearLayout>
</RelativeLayout>

Ⅳ 如何打造Android自定义的下拉列表框控件

实现方式:
1、水平布局一个TextView和一个ImageView(小黑箭头)
2、实现点击ImageView的单击事件,弹出PopupWindow
3、PopupWindow中实现下拉列表
关键代码示例:
1、布局

<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView/>
<ImageView />
</LinearLayout>
2、单击事件

image.setBackgroundResource(R.drawable.gerendang_jiantou);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//弹出popupwindow
}
});
3、pupupwindow相关代码
ListView lv = new ListView(this);
adapter = new OptionsAdapter(context, datas); // 根据数据,设置下拉框显示
list.setAdapter(adapter);

/**
* 两种不同长度的下拉框,主要是为了适应屏幕的大小
*/
if (p_width > 0) {
pWindow = new PopupWindow(v, par.getWidth(), 150);
} else {
pWindow = new PopupWindow(v, par.getWidth(), 300);
}
pWindow.setFocusable(true); //能够焦点获得
pWindow.setBackgroundDrawable(new BitmapDrawable()); //设置背景
pWindow.setOutsideTouchable(true); //外部点击关闭
pWindow.update(); //更新位置

Ⅳ android使用百度地图3.0版本怎样实现自定义弹出窗口功能

基本原理就是用ItemizedOverlay来添加附加物,在OnTap方法中向MapView上添加一个自定义的View(如果已存在就直接设为可见),下面具体来介绍我的实现方法:


一、自定义覆盖物类:MyPopupOverlay,这个类是最关键的一个类ItemizedOverlay,用于设置Marker,并定义Marker的点击事件,弹出窗口,至于弹出窗口的内容,则通过定义Listener,放到Activity中去构造。如果没有特殊需求,这个类不需要做什么改动。代码如下,popupLinear这个对象,就是加到地图上的自定义View:

<OverlayItem>{
privateContextcontext=null;
//这是弹出窗口,包括内容部分还有下面那个小三角
=null;
//这是弹出窗口的内容部分
private
ViewpopupView=null;
privateMapViewmapView=null;
private
Projectionprojection=null;
//这是弹出窗口内容部分使用的layoutId,在Activity中设置
privateintlayoutId=
0;
//是否使用网络带有A-J字样的Marker
=
false;
privateint[]defaultMarkerIds={
R.drawable.icon_marka,
R.drawable.icon_markb,
R.drawable.icon_markc,
R.drawable.icon_markd,
R.drawable.icon_marke,
R.drawable.icon_markf,
R.drawable.icon_markg,
R.drawable.icon_markh,
R.drawable.icon_marki,
R.drawable.icon_markj,};
//这个Listener用于在Marker被点击时让Activity填充PopupView的内容
private
OnTapListeneronTapListener=null;
publicMyPopupOverlay(Contextcontext,Drawablemarker,MapViewmMapView)
{
super(marker,mMapView);
this.context=
context;
this.popupLinear=newLinearLayout(context);
this.mapView=mMapView;
popupLinear.setOrientation(LinearLayout.VERTICAL);
popupLinear.setVisibility(View.GONE);
projection=
mapView.getProjection();
}
@Override
publicbooleanonTap(GeoPointpt,MapViewmMapView)
{
//点击窗口以外的区域时,当前窗口关闭
if(popupLinear!=null&&
popupLinear.getVisibility()==View.VISIBLE){
LayoutParamslp=
(LayoutParams)popupLinear.getLayoutParams();
PointtapP=new
Point();
projection.toPixels(pt,tapP);
PointpopP
=newPoint();
projection.toPixels(lp.point,
popP);
intxMin=popP.x-lp.width/2+lp.x;
intyMin=popP.y-lp.height+lp.y;
intxMax=popP.x+
lp.width/2+lp.x;
intyMax=popP.y+lp.y;
if
(tapP.x<xMin||tapP.y<yMin||tapP.x>xMax
||tapP.y>yMax)
popupLinear.setVisibility(View.GONE);
}
return
false;
}
@Override
protectedbooleanonTap(inti){
//
点击Marker时,该Marker滑动到地图中央偏下的位置,并显示Popup窗口
OverlayItemitem=
getItem(i);
if(popupView==null){
//
如果popupView还没有创建,则构造popupLinear
if
(!createPopupView()){
returntrue;
}
}
if(onTapListener==null)
return
true;
popupLinear.setVisibility(View.VISIBLE);
onTapListener.onTap(i,popupView);
popupLinear.measure(0,0);
intviewWidth=
popupLinear.getMeasuredWidth();
intviewHeight=
popupLinear.getMeasuredHeight();
LayoutParamslayoutParams=newLayoutParams(viewWidth,
viewHeight,
item.getPoint(),0,-60,
LayoutParams.BOTTOM_CENTER);
layoutParams.mode=
LayoutParams.MODE_MAP;
popupLinear.setLayoutParams(layoutParams);
Pointp=new
Point();
projection.toPixels(item.getPoint(),p);
p.y=
p.y-viewHeight/2;
GeoPointpoint=projection.fromPixels(p.x,
p.y);
mapView.getController().animateTo(point);
return
true;
}
privatebooleancreatePopupView(){
//TODOAuto-generated
methodstub
if(layoutId==0)
return
false;
popupView=LayoutInflater.from(context).inflate(layoutId,
null);
popupView.setBackgroundResource(R.drawable.popupborder);
ImageView
dialogStyle=newImageView(context);
dialogStyle.setImageDrawable(context.getResources().getDrawable(
R.drawable.iw_tail));
popupLinear.addView(popupView);
android.widget.LinearLayout.LayoutParamslp=new
android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
lp.topMargin=
-2;
lp.leftMargin=60;
popupLinear.addView(dialogStyle,
lp);
mapView.addView(popupLinear);
returntrue;
}
@Override
publicvoidaddItem(List<OverlayItem>items)
{
//TODOAuto-generatedmethodstub
intstartIndex=
getAllItem().size();
for(OverlayItemitem:items){
if(startIndex>=defaultMarkerIds.length)
startIndex=
defaultMarkerIds.length-1;
if(useDefaultMarker&&
item.getMarker()==null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[startIndex++]));
}
}
super.addItem(items);
}
@Override
publicvoidaddItem(OverlayItemitem){
//
TODOAuto-generatedmethodstub
//
重载这两个addItem方法,主要用于设置自己默认的Marker
intindex=
getAllItem().size();
if(index>=
defaultMarkerIds.length)
index=defaultMarkerIds.length-
1;
if(useDefaultMarker&&item.getMarker()==
null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[getAllItem().size()]));
}
super.addItem(item);
}
publicvoidsetLayoutId(intlayoutId){
this.layoutId=
layoutId;
}
publicvoidsetUseDefaultMarker(booleanuseDefaultMarker){
this.useDefaultMarker=useDefaultMarker;
}
publicvoidsetOnTapListener(OnTapListeneronTapListener){
this.onTapListener=onTapListener;
}
publicinterfaceOnTapListener{
publicvoidonTap(intindex,
ViewpopupView);
}
}

二、MainActivity,这是主界面,用来显示地图,创建MyPopupOverlay对象,在使用我写的MyPopupOverlay这个类时,需要遵循以下步骤:


创建MyPopupOverlay对象,构造函数为public MyPopupOverlay(Context context, Drawable marker, MapView mMapView),四个参数分别为当前的上下文、通用的Marker(这是ItemizedOverlay需要的,当不设置Marker时的默认Marker)以及网络地图对象。

设置自定义的弹出窗口内容的布局文件ID,使用的方法为public void setLayoutId(int layoutId)。

设置是使用自定义的Marker,还是预先写好的带有A-J字样的网络地图原装Marker,使用的方法为public void setUseDefaultMarker(boolean useDefaultMarker),只有当这个值为true且没有调用OverlayItem的setMarker方法为特定点设置Marker时,才使用原装Marker。

创建Marker所在的点,即分别创建一个个OverlayItem,然后调用public void addItem(OverlayItem item)或public void addItem(List<OverlayItem> items)方法来把这些OverlayItem添加到自定义的附加层上去。

为MyPopupOverlay对象添加onTap事件,当Marker被点击时,填充弹出窗口中的内容(也就是第2条中layoutId布局中的内容),设置方法为public void setOnTapListener(OnTapListener onTapListener),OnTapListener是定义在MyPopupOverlay中的接口,实现这个接口需要覆写public void onTap(int index, View popupView)方法,其中,index表示被点击的Marker(确切地说是OverlayItem)的索引,popupView是使用layoutId这个布局的View,也就是弹出窗口除了下面的小三角之外的部分。

把这个MyPopupOverlay对象添加到地图上去:mMapView.getOverlays().add(myOverlay);mMapView.refresh();

Ⅵ android自定义弹出框样式

AlertDialog.Builder dial = new AlertDialog.Builder(mContext);
...
dial.setView(layout).dial.create().show();

Ⅶ android开发百度地图怎么实现自定义弹出窗口

基本原理就是用ItemizedOverlay来添加附加物,在OnTap方法中向MapView上添加一个自定义的View(如果已存在就直接设为可见),下面具体来介绍我的实现方法:
一、自定义覆盖物类:MyPopupOverlay,这个类是最关键的一个类ItemizedOverlay,用于设置Marker,并定义Marker的点击事件,弹出窗口,至于弹出窗口的内容,则通过定义Listener,放到Activity中去构造。如果没有特殊需求,这个类不需要做什么改动。代码如下,popupLinear这个对象,就是加到地图上的自定义View:
public class MyPopupOverlay extends ItemizedOverlay<OverlayItem> {

private Context context = null;
// 这是弹出窗口, 包括内容部分还有下面那个小三角
private LinearLayout popupLinear = null;
// 这是弹出窗口的内容部分
private View popupView = null;
private MapView mapView = null;
private Projection projection = null;
// 这是弹出窗口内容部分使用的layoutId,在Activity中设置
private int layoutId = 0;
// 是否使用网络带有A-J字样的Marker
private boolean useDefaultMarker = false;
private int[] defaultMarkerIds = { R.drawable.icon_marka,
R.drawable.icon_markb, R.drawable.icon_markc,
R.drawable.icon_markd, R.drawable.icon_marke,
R.drawable.icon_markf, R.drawable.icon_markg,
R.drawable.icon_markh, R.drawable.icon_marki,
R.drawable.icon_markj, };
// 这个Listener用于在Marker被点击时让Activity填充PopupView的内容
private OnTapListener onTapListener = null;
public MyPopupOverlay(Context context, Drawable marker, MapView mMapView) {
super(marker, mMapView);
this.context = context;
this.popupLinear = new LinearLayout(context);
this.mapView = mMapView;
popupLinear.setOrientation(LinearLayout.VERTICAL);
popupLinear.setVisibility(View.GONE);
projection = mapView.getProjection();
}
@Override
public boolean onTap(GeoPoint pt, MapView mMapView) {
// 点击窗口以外的区域时,当前窗口关闭
if (popupLinear != null && popupLinear.getVisibility() == View.VISIBLE) {
LayoutParams lp = (LayoutParams) popupLinear.getLayoutParams();
Point tapP = new Point();
projection.toPixels(pt, tapP);
Point popP = new Point();
projection.toPixels(lp.point, popP);
int xMin = popP.x - lp.width / 2 + lp.x;
int yMin = popP.y - lp.height + lp.y;
int xMax = popP.x + lp.width / 2 + lp.x;
int yMax = popP.y + lp.y;
if (tapP.x < xMin || tapP.y < yMin || tapP.x > xMax
|| tapP.y > yMax)
popupLinear.setVisibility(View.GONE);
}
return false;
}
@Override
protected boolean onTap(int i) {
// 点击Marker时,该Marker滑动到地图中央偏下的位置,并显示Popup窗口
OverlayItem item = getItem(i);
if (popupView == null) {
// 如果popupView还没有创建,则构造popupLinear
if (!createPopupView()){
return true;
}
}
if (onTapListener == null)
return true;
popupLinear.setVisibility(View.VISIBLE);
onTapListener.onTap(i, popupView);
popupLinear.measure(0, 0);
int viewWidth = popupLinear.getMeasuredWidth();
int viewHeight = popupLinear.getMeasuredHeight();
LayoutParams layoutParams = new LayoutParams(viewWidth, viewHeight,
item.getPoint(), 0, -60, LayoutParams.BOTTOM_CENTER);
layoutParams.mode = LayoutParams.MODE_MAP;
popupLinear.setLayoutParams(layoutParams);
Point p = new Point();
projection.toPixels(item.getPoint(), p);
p.y = p.y - viewHeight / 2;
GeoPoint point = projection.fromPixels(p.x, p.y);
mapView.getController().animateTo(point);
return true;
}
private boolean createPopupView() {
// TODO Auto-generated method stub
if (layoutId == 0)
return false;
popupView = LayoutInflater.from(context).inflate(layoutId, null);
popupView.setBackgroundResource(R.drawable.popupborder);
ImageView dialogStyle = new ImageView(context);
dialogStyle.setImageDrawable(context.getResources().getDrawable(
R.drawable.iw_tail));
popupLinear.addView(popupView);
android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.topMargin = -2;
lp.leftMargin = 60;
popupLinear.addView(dialogStyle, lp);
mapView.addView(popupLinear);
return true;
}
@Override
public void addItem(List<OverlayItem> items) {
// TODO Auto-generated method stub
int startIndex = getAllItem().size();
for (OverlayItem item : items){
if (startIndex >= defaultMarkerIds.length)
startIndex = defaultMarkerIds.length - 1;
if (useDefaultMarker && item.getMarker() == null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[startIndex++]));
}
}
super.addItem(items);
}
@Override
public void addItem(OverlayItem item) {
// TODO Auto-generated method stub
// 重载这两个addItem方法,主要用于设置自己默认的Marker
int index = getAllItem().size();
if (index >= defaultMarkerIds.length)
index = defaultMarkerIds.length - 1;
if (useDefaultMarker && item.getMarker() == null){
item.setMarker(context.getResources().getDrawable(
defaultMarkerIds[getAllItem().size()]));
}
super.addItem(item);
}
public void setLayoutId(int layoutId) {
this.layoutId = layoutId;
}
public void setUseDefaultMarker(boolean useDefaultMarker) {
this.useDefaultMarker = useDefaultMarker;
}
public void setOnTapListener(OnTapListener onTapListener) {
this.onTapListener = onTapListener;
}
public interface OnTapListener {
public void onTap(int index, View popupView);
}
}

Ⅷ 安卓开发 自定义界面的弹窗

查看 api demo 中的 api 菜单下的 dialog 示例,随便找一个就可以满足你的需求。

Ⅸ android怎么在自定义控件中弹出对话框!!!

直接new一个AlertDialog就可以了啊
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("111");
builder.setMessage("222");
final AlertDialog dialog = builder.create();
dialog.show();

Ⅹ android怎样自定义dialog

Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作,
如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作。
Android系统提供了Dialog类,以及Dialog的子类,常见如AlertDialog来实现此类功能。
一般情况下,利用Android提供的Dialog及其子类能够满足多数此类需求,然而,其不足之处体现在:
1. 基于Android提供的Dialog及其子类样式单一,风格上与App本身风格可能不太协调;
2. Dialog弹窗在布局和功能上有所限制,有时不一定能满足实际的业务需求。
本文将通过在Dialog基础上构建自定义的Dialog弹窗,以最常见的确认弹框为例。
本样式相对比较简单:上面有一个弹框标题(提示语),下面左右分别是“确认”和“取消”按钮,当用户点击“确认”按钮时,弹框执行
相应的确认逻辑,当点击“取消”按钮时,执行相应的取消逻辑。
首先,自定义弹框样式:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="wrap_content"
5 android:background="@drawable/dialog_bg"
6 android:orientation="vertical" >
7
8 <TextView
9 android:id="@+id/title"
10 android:layout_width="wrap_content"
11 android:layout_height="wrap_content"
12 android:layout_gravity="center"
13 android:paddingTop="14dp"
14 android:textColor="@color/login_hint"
15 android:textSize="@dimen/text_size_18" />
16
17 <LinearLayout
18 android:layout_width="match_parent"
19 android:layout_height="wrap_content"
20 android:layout_marginBottom="14dp"
21 android:layout_marginLeft="20dp"
22 android:layout_marginRight="20dp"
23 android:layout_marginTop="30dp" >
24
25 <TextView
26 android:id="@+id/confirm"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:layout_marginRight="10dp"
30 android:layout_weight="1"
31 android:background="@drawable/btn_confirm_selector"
32 android:gravity="center"
33 android:textColor="@color/white"
34 android:textSize="@dimen/text_size_16" />
35
36 <TextView
37 android:id="@+id/cancel"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:layout_marginLeft="10dp"
41 android:layout_weight="1"
42 android:background="@drawable/btn_cancel_selector"
43 android:gravity="center"
44 android:textColor="@color/login_hint"
45 android:textSize="@dimen/text_size_16" />
46 </LinearLayout>
47
48 </LinearLayout>

然后,通过继承Dialog类构建确认弹框控件ConfirmDialog:

1 package com.corn.widget;
2
3 import android.app.Dialog;
4 import android.content.Context;
5 import android.os.Bundle;
6 import android.util.DisplayMetrics;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.Window;
10 import android.view.WindowManager;
11 import android.widget.TextView;
12
13 import com.corn.R;
14
15 public class ConfirmDialog extends Dialog {
16
17 private Context context;
18 private String title;
19 private String confirmButtonText;
20 private String cacelButtonText;
21 private ClickListenerInterface clickListenerInterface;
22
23 public interface ClickListenerInterface {
24
25 public void doConfirm();
26
27 public void doCancel();
28 }
29
30 public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) {
31 super(context, R.style.MyDialog);
32 this.context = context;
33 this.title = title;
34 this.confirmButtonText = confirmButtonText;
35 this.cacelButtonText = cacelButtonText;
36 }
37
38 @Override
39 protected void onCreate(Bundle savedInstanceState) {
40 // TODO Auto-generated method stub
41 super.onCreate(savedInstanceState);
42
43 init();
44 }
45
46 public void init() {
47 LayoutInflater inflater = LayoutInflater.from(context);
48 View view = inflater.inflate(R.layout.confirm_dialog, null);
49 setContentView(view);
50
51 TextView tvTitle = (TextView) view.findViewById(R.id.title);
52 TextView tvConfirm = (TextView) view.findViewById(R.id.confirm);
53 TextView tvCancel = (TextView) view.findViewById(R.id.cancel);
54
55 tvTitle.setText(title);
56 tvConfirm.setText(confirmButtonText);
57 tvCancel.setText(cacelButtonText);
58
59 tvConfirm.setOnClickListener(new clickListener());
60 tvCancel.setOnClickListener(new clickListener());
61
62 Window dialogWindow = getWindow();
63 WindowManager.LayoutParams lp = dialogWindow.getAttributes();
64 DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
65 lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
66 dialogWindow.setAttributes(lp);
67 }
68
69 public void setClicklistener(ClickListenerInterface clickListenerInterface) {
70 this.clickListenerInterface = clickListenerInterface;
71 }
72
73 private class clickListener implements View.OnClickListener {
74 @Override
75 public void onClick(View v) {
76 // TODO Auto-generated method stub
77 int id = v.getId();
78 switch (id) {
79 case R.id.confirm:
80 clickListenerInterface.doConfirm();
81 break;
82 case R.id.cancel:
83 clickListenerInterface.doCancel();
84 break;
85 }
86 }
87
88 };
89
90 }

在如上空间构造代码中,由于控件的"确认"和"取消"逻辑与实际的应用场景有关,因此,控件中通过定义内部接口来实现。

在需要使用此控件的地方,进行如下形式调用:

1 public static void Exit(final Context context) {
2 final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定要退出吗?", "退出", "取消");
3 confirmDialog.show();
4 confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
5 @Override
6 public void doConfirm() {
7 // TODO Auto-generated method stub
8 confirmDialog.dismiss();
9 //toUserHome(context);
10 AppManager.getAppManager().AppExit(context);
11 }
12
13 @Override
14 public void doCancel() {
15 // TODO Auto-generated method stub
16 confirmDialog.dismiss();
17 }
18 });
19 }

调用中实现了此控件的内部接口,并赋给控件本身,以此在点击按钮时实现基于外部具体业务逻辑的函数回调。

热点内容
爱奇艺会员怎么改密码 发布:2025-03-06 11:33:44 浏览:58
firefox不缓存 发布:2025-03-06 11:33:43 浏览:464
淘宝密码如何破解 发布:2025-03-06 11:32:56 浏览:591
sqlservereclipse 发布:2025-03-06 11:25:29 浏览:704
linux存放文件 发布:2025-03-06 11:24:47 浏览:444
nfslinux挂载 发布:2025-03-06 11:19:42 浏览:233
安卓动态壁纸怎么提取 发布:2025-03-06 11:07:26 浏览:111
有锁安卓手机有什么坏处 发布:2025-03-06 11:00:20 浏览:575
dvwa上传 发布:2025-03-06 10:46:58 浏览:699
新款荣放低配有哪些新配置 发布:2025-03-06 10:41:29 浏览:791