android自定義彈出窗
Ⅰ 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 }
調用中實現了此控制項的內部介面,並賦給控制項本身,以此在點擊按鈕時實現基於外部具體業務邏輯的函數回調。