android TV开发:弹出菜单实现

1.在有时候我们需要选择排序方式,在电视开发中往往使用一个弹出菜单实现,Demo效果:

android TV开发:弹出菜单实现_第1张图片

2.核心代码:

public class MainActivity extends Activity {
    private PopupWindow popupWindow;
    private View contentView;
    private Button btn;
//...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//...
        createPopWindow();//初始化弹出界面

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //从底部显示
                popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
            }
        });

    }

    private void createPopWindow() {
        contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_window, null);
        //设置弹出框的宽度和高度
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, 180);
        popupWindow.setFocusable(true);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(true);
        //设置可以点击
        popupWindow.setTouchable(true);
        //进入退出的动画
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);

		
		reMen = (Button)contentView.findViewById(R.id.reMen);
        zuiXin = (Button)contentView.findViewById(R.id.zuiXin);
        pingFen = (Button)contentView.findViewById(R.id.pingFen);
        //是否选中的小图标
        img = (ImageView)contentView.findViewById(R.id.img);
        img2 = (ImageView)contentView.findViewById(R.id.img2);
        img3 = (ImageView)contentView.findViewById(R.id.img3);
        img2.setVisibility(View.INVISIBLE);
        img3.setVisibility(View.INVISIBLE);
        reMen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img.setVisibility(View.VISIBLE);
                img2.setVisibility(View.INVISIBLE);
                img3.setVisibility(View.INVISIBLE);

            }
        });
        pingFen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img2.setVisibility(View.VISIBLE);
                img.setVisibility(View.INVISIBLE);
                img3.setVisibility(View.INVISIBLE);

            }
        });
        zuiXin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img3.setVisibility(View.VISIBLE);
                img2.setVisibility(View.INVISIBLE);
                img.setVisibility(View.INVISIBLE);

            }
        });

    }

}
3.demo源码+apk  下载

你可能感兴趣的:(android,TV)