popWindowdemo

6964 단어 androidpopwindow
xml 파일
1.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="horizontal" 
   >

    <LinearLayout
        android:id="@+id/title_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/bg"
         android:gravity="center">
        <TextView
            android:id="@+id/title"
            android:text="@string/str_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textSize="@dimen/size_title" 
            android:textColor="@android:color/white"/>

        <ImageView
            android:id="@+id/arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dip"
            android:background="@drawable/icon_arrow_down"
       />
    </LinearLayout>

</LinearLayout>

2.pop.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<!--   
	<ListView
		android:id="@+id/pop_list"
    	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
		android:dividerHeight="2dip" 
		android:cacheColorHint="#00000000"
    	></ListView>
  --> 
  <TextView 
      	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:textColor="#000000"
    	android:text="  :"
      />
  <EditText 
      	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:lines="1"
      />
   <TextView 
      	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:textColor="#000000"
    	android:text="  :"
      />
  <EditText 
      	android:layout_width="fill_parent" 
    	android:layout_height="wrap_content"
    	android:lines="1"
      />
  <Button 
      	android:layout_width="wrap_content" 
    	android:layout_height="wrap_content"
    	android:text="     "
      />
  
</LinearLayout>

3.(activity)PopwindowActivity.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class PopwindowActivity extends Activity
{

	private ImageView arrow;
	private boolean isOpenPop = false;
	private PopupWindow window;
	private ListView list;
	private LinearLayout title_layout;
	public static final String KEY = "key";
	ArrayList<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
	Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mContext = this;
		arrow = (ImageView) findViewById(R.id.arrow);
		title_layout = (LinearLayout) findViewById(R.id.title_layout);
		title_layout.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub

				changPopState(v);

			}
		});
	}

	/**
	 *   Pop  
	 * */

	public void changPopState(View v)
	{

		isOpenPop = !isOpenPop;
		if (isOpenPop)
		{
			arrow.setBackgroundResource(R.drawable.icon_arrow_up);
			popAwindow(v);

		} else
		{
			arrow.setBackgroundResource(R.drawable.icon_arrow_down);
			if (window != null)
			{
				window.dismiss();
			}
		}
	}

	private void popAwindow(View parent)
	{
		if (window == null)
		{
			LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = lay.inflate(R.layout.pop, null);
			
			window = new PopupWindow(v, 260, 300);
			int x = (int) getResources().getDimension(R.dimen.pop_x);
			int y = (int) getResources().getDimension(R.dimen.pop_y);
			window = new PopupWindow(v, x, y);
		}
		window.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.pop_bg));
		window.setFocusable(true);
		window.setOutsideTouchable(false);
		window.setOnDismissListener(new OnDismissListener()
		{

			@Override
			public void onDismiss()
			{
				// TODO Auto-generated method stub
				isOpenPop = false;
				arrow.setBackgroundResource(R.drawable.icon_arrow_down);
			}
		});
		window.update();
		window.showAtLocation(parent, Gravity.CENTER_HORIZONTAL | Gravity.TOP,
				0, (int) getResources().getDimension(R.dimen.pop_layout_y));

	}
}

좋은 웹페이지 즐겨찾기