欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android top弹出框

程序员文章站 2022-07-15 12:50:09
...

android top弹出框的实现原理:当要弹出菜单的时候将隐藏的linearlayout以动画的形式显示出来,隐藏菜单时以动画的形式隐藏linearlayout。想到了,其实就很简单的。

 

 

<?xml version="1.0" encoding="utf-8" ?> 
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> 
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="显示/隐藏菜单" /> 
- <LinearLayout android:id="@+id/menu" android:layout_width="fill_parent" android:layout_height="100dp">
  <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="哈哈哈哈哈哈哈哈哈哈" android:gravity="center" /> 
  </LinearLayout>
  </LinearLayout>

 其中可显示隐藏的linearlayout 布局时可*发挥

 

 

package com.cng;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

public class AndroidexampleActivity extends Activity {
	//显示   隐藏   动画
	Animation showaction,hideaction;
	//显示隐藏操作的对象
	LinearLayout menu;
	Button button;
	Boolean menushowed;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        menu=(LinearLayout)findViewById(R.id.menu);
        menu.setBackgroundColor(Color.WHITE);
        button=(Button)findViewById(R.id.button);
        //显示动画
        showaction=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 
        		0.0f, Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF,-1.0f,
        		Animation.RELATIVE_TO_SELF,0.0f);
        showaction.setDuration(500);
        //隐藏动画
        hideaction=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 
        		0.0f, Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF,0.0f,
        		Animation.RELATIVE_TO_SELF,-1.0f);
        hideaction.setDuration(500);
        menushowed=false;
        menu.setVisibility(View.GONE);
        button.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				if(menushowed)
				{
					menushowed=false;
					menu.startAnimation(hideaction);
					menu.setVisibility(View.GONE);
				}
				else 
				{
					menushowed=true;
					menu.startAnimation(showaction);
					menu.setVisibility(View.VISIBLE);
				}
			}
		});
    }
}