一个更多按钮popwindow

实现效果,如下图。
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:gravity="center_horizontal"
    android:orientation="vertical" >


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/icon_margin_top"
                android:background="@drawable/menu_sharepage" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="分享好友"
                android:textColor="#FFF"
                android:textSize="@dimen/text_size_ten" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/icon_margin_top"
                android:background="@drawable/menu_bookmark" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="意见反馈"
                android:textColor="#FFF"
                android:textSize="@dimen/text_size_ten" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/icon_margin_top"
                android:background="@drawable/menu_bookmark_sync_import" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="版本更新"
                android:textColor="#FFF"
                android:textSize="@dimen/text_size_ten" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/icon_margin_top"
                android:background="@drawable/menu_about" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="关于"
                android:textColor="#FFF"
                android:textSize="@dimen/text_size_ten" />
        </LinearLayout>
    </LinearLayout>
    
    <!-- 第二排 -->
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                android:background="@drawable/menu_quit" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="退出"
                android:textColor="#FFF"
                android:textSize="@dimen/text_size_ten" />
        </LinearLayout>
         <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                />

        </LinearLayout>
         <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

              <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                />
        </LinearLayout>
         <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="@dimen/layout_size"
            android:layout_height="@dimen/layout_size"
            android:orientation="vertical" >

              <ImageView
                android:layout_width="@dimen/icon_size"
                android:layout_height="@dimen/icon_size"
                android:layout_gravity="center"
                />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


java文件
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.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

public class MainActivity extends Activity
{
	private boolean isOpenPop = false;
	private PopupWindow window;
	private Button moreBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		moreBtn = (Button) findViewById(R.id.btn);
		moreBtn.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				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.more_pop_item, null);
			
			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.preview_button));
		window.getBackground().setAlpha(150);
		window.setFocusable(true);
		window.setOutsideTouchable(false);
		window.setOnDismissListener(new OnDismissListener()
		{
			public void onDismiss()
			{
				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));
	}
}


你可能感兴趣的:(window)