android PopupWindow




 



<?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:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:background="@color/grey" >

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:drawable/divider_horizontal_bright" />

    <TextView
        android:id="@+id/tv_all_state"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ECF0F2"
        android:gravity="center"
        android:padding="10dp"
        android:text="全部"
        android:textColor="@color/black" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:drawable/divider_horizontal_bright" />

    <TextView
        android:id="@+id/tv_done"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ECF0F2"
        android:gravity="center"
        android:padding="10dp"
        android:text="已完成"
        android:textColor="@color/black" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:drawable/divider_horizontal_bright" />

    <TextView
        android:id="@+id/tv_undo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ECF0F2"
        android:gravity="center"
        android:padding="10dp"
        android:text="未完成"
        android:textColor="@color/black" />

</LinearLayout>
package com.example.demopopupwindow.activity;
 

import com.example.demopopupwindow.R; 

import android.app.Activity; 
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle; 
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class PopupWindowActivity extends Activity implements OnClickListener{

	 private TextView                   mTopChooseBar;
	 private PopupWindow                mPopupWindow;
	 private TextView                   mTvDone; 
	 private TextView                   mTvUnDo; 
	 private TextView                   mTvAllState;
	 
	 
	  @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

	    mTopChooseBar = (TextView) this.findViewById( R.id.tv_topbar);
	    mTopChooseBar.setOnClickListener(this);
        setOnChooseListener();
		
	}
	  
	 
	 
	 public void setOnChooseListener() {
		View view = View.inflate( this, R.layout.topbar, null );
        mTvDone = ( TextView ) view.findViewById( R.id.tv_done );
        mTvUnDo = ( TextView ) view.findViewById( R.id.tv_undo );
        mTvAllState = ( TextView ) view.findViewById( R.id.tv_all_state );

        mPopupWindow = new PopupWindow(view ,LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT, false );
        mPopupWindow.setBackgroundDrawable( new BitmapDrawable() );
        mPopupWindow.setOutsideTouchable( true );
        mPopupWindow.setFocusable( true );

        mTopChooseBar.setOnClickListener( this );
        mTvDone.setOnClickListener( this );
        mTvUnDo.setOnClickListener( this );
        mTvAllState.setOnClickListener( this );
	    }

	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.tv_topbar:
			if ( mPopupWindow.isShowing() ) {
                mPopupWindow.dismiss();
            }
            else {
                mPopupWindow.showAsDropDown( v );
            }
			break;
		case R.id.tv_done:
			mTopChooseBar.setText( mTvDone.getText() );
            mPopupWindow.dismiss();
			break;
		case R.id.tv_undo:
			 mTopChooseBar.setText( mTvUnDo.getText() );
             mPopupWindow.dismiss();
			break;
		case R.id.tv_all_state:
			 mTopChooseBar.setText( mTvAllState.getText() );
             mPopupWindow.dismiss();
			break;
	
		default:
			break;
		}
	}
	 
}
扫描二维码关注公众号,回复: 305919 查看本文章

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

猜你喜欢

转载自knight-black-bob.iteye.com/blog/2296435