Android仿相册点击屏幕出现标题和底部菜单

自己在学习时实现的一个简单的效果,有点像平时管理相册图片时点一下屏幕就出来的两边菜单。

具体效果如下:




点击屏幕后出现菜单



点击了菜单中的按钮


主要就是使用了PopupWindow,注意要给每个PopupWindow加上setFocusable(false)和setOutsideTouchable(true),这样才能在菜单出现后还可以点击屏幕使其消失。

具体代码:

package com.example.kid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class NewDraw extends Activity
{
	private boolean isShow = false;
	private boolean isSee = true;
	
	private PopupWindow title; 
	private PopupWindow menu;
	private LayoutInflater inflaterTitle; 
	private LayoutInflater inflaterMenu;
	private View layoutTitle; 
	private View layoutMenu;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN,WindowManager.LayoutParams. FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.draw_newdraw);
        
        //获取LayoutInflater,用于载入layout下的布局文件,类似于findViewById()
        inflaterTitle  = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); 
        inflaterMenu  = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE); 
        //载入PopupWindow的布局 
        View popTitleView = getLayoutInflater().inflate(R.layout.draw_newdrawpoptitle, null, false); 
        View popMenuView = getLayoutInflater().inflate(R.layout.draw_newdrawpopmenu, null, false); 
        
        final ImageButton seeButton = (ImageButton)popMenuView.findViewById(R.id.ImageButton03);
        seeButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)   
        	{  
        		if(isSee == true)
        		{
        			seeButton.setImageResource(R.drawable.draw_nosee);
        			Toast.makeText(getApplicationContext(), "弹幕评论不可见", Toast.LENGTH_SHORT).show(); 
        			isSee = false;
        		}
        		else
        		{
        			seeButton.setImageResource(R.drawable.draw_see);
        			Toast.makeText(getApplicationContext(), "弹幕评论可见", Toast.LENGTH_SHORT).show();
        			isSee = true;
        		}
        	}
        });
        
        TextView imageTitle = (TextView)popTitleView.findViewById(R.id.textView1);
        
        //设置PopupWindow的布局 
        menu =new PopupWindow(popMenuView, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
        title =new PopupWindow(popTitleView, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
           
        ImageView newImage = (ImageView)findViewById(R.id.image);
        newImage.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)   
        	{  
        		if(isShow == true)
        		{
        			title.dismiss();
        			menu.dismiss();
        			isShow = false;
        		}
        		else
        		{
        			menu.showAtLocation(v, Gravity.BOTTOM,0,0); //设置menu位置在底部
        			menu.setFocusable(false);
        			menu.setOutsideTouchable(true);
        			menu.update();
        			
        			title.showAtLocation(v, Gravity.TOP,0,0); //设置title位置在顶部
        			title.setFocusable(false);
        			title.setOutsideTouchable(true);
        			title.update();
        			isShow = true;
        		}
        	}
        });
	newImage.setImageResource(R.drawable.draw6);
        imageTitle.setText("斑马");       
    }
    
}

draw_newdrawpoptitle布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"
            android:text="                  "
            android:textColor="#ffffff"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>

</FrameLayout>


draw_newdrawpopmenu布局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="28dp"
            android:layout_height="25dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="4dp"
            android:layout_toRightOf="@+id/ImageButton03"
            android:background="#000000"
            android:scaleType="centerCrop"
            android:src="@drawable/draw_comment" />

        <ImageButton
            android:id="@+id/ImageButton01"
            android:layout_width="30dp"
            android:layout_height="27dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="2dp"
            android:layout_toRightOf="@+id/imageButton1"
            android:background="#000000"
            android:scaleType="centerCrop"
            android:src="@drawable/draw_like" />

        <ImageButton
            android:id="@+id/ImageButton02"
            android:layout_width="26dp"
            android:layout_height="26dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="3dp"
            android:layout_toRightOf="@+id/ImageButton01"
            android:background="#000000"
            android:scaleType="centerCrop"
            android:src="@drawable/draw_save" />

        <ImageButton
            android:id="@+id/ImageButton03"
            android:layout_width="30dp"
            android:layout_height="22dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="45dp"
            android:layout_marginTop="4dp"
            android:layout_toRightOf="@+id/textView1"
            android:background="#000000"
            android:scaleType="centerCrop"
            android:src="@drawable/draw_see" />

    </RelativeLayout>

</FrameLayout>
这里面的几个ImageButton都是一些小图标,替换一下就好了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值