Android PopupWindow实现,类似于iOS的选择栏

简介:

转载请注明出处:王亟亟的大牛之路

以前项目中也做过类似的东西,今天想到就写一篇关于这样的东西了,外面这一类的实现也很多,都可以借鉴,但是,谢了也就写了,嗯!!
项目结构:
这里写图片描述

运行效果
这里写图片描述

完成了布局的圆角,弹出弹出的动画以及一系列监听事件

主Activity

public class MainActivity extends Activity implements OnClickListener{
    Button showPopupWindow;
    TextView mServerLogin;
    TextView mSmsLogin;
    TextView mSmsCancel;
    View root;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showPopupWindow=(Button)findViewById(R.id.showPopupWindow);
        showPopupWindow.setOnClickListener(this);
     // 父窗口view
        root = findViewById(R.id.root);
        //初始化
        initPopupWindow();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.showPopupWindow:
                popupWindow.showAtLocation(root, Gravity.BOTTOM, 0, 0);
            break;
            case R.id.mServerLogin:
                Toast.makeText(MainActivity.this, "登录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.mSmsLogin:
                Toast.makeText(MainActivity.this, "注册", Toast.LENGTH_SHORT).show();
                break;
            case R.id.mSmsCancel:
                if (popupWindow.isShowing())
                    popupWindow.dismiss();
                break;
        default:
            break;
        }
    }

    public void initPopupWindow(){
        View view=getLayoutInflater().inflate(R.layout.dialog, null);
        mServerLogin = (TextView) view.findViewById(R.id.mServerLogin);
        mSmsLogin = (TextView) view.findViewById(R.id.mSmsLogin);
        mSmsCancel= (TextView) view.findViewById(R.id.mSmsCancel);
        mServerLogin.setOnClickListener(this);
        mSmsLogin.setOnClickListener(this);
        mSmsCancel.setOnClickListener(this);
        // 全屏显示,将内容设置在底部
        popupWindow = new PopupWindow(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setAnimationStyle(R.style.pop_animation);
    }
}

主布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.wjj.popupwindowdemo.MainActivity" 
    android:id="@+id/root"
    android:background="#DEB887">

    <Button
        android:id="@+id/showPopupWindow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="showPopupWindow" 
        android:background="#40E0D0"/>
</LinearLayout>

弹出内容的布局:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical" 
            android:background="@drawable/shape">

            <TextView
                android:id="@+id/mServerLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:text="登陆"
                android:textColor="#3995e1"
                android:textSize="16sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#999999" >
            </LinearLayout>

            <TextView
                android:id="@+id/mSmsLogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:text="注册"
                android:textColor="#3995e1"
                android:textSize="16sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#999999" >
            </LinearLayout>

            <TextView
                android:id="@+id/mSmsCancel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingBottom="8dp"
                android:paddingTop="8dp"
                android:text="取消"
                android:textColor="#3995e1"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

圆角的shape:xml

  <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
      <!-- 填充的颜色 -->
      <solid android:color="@color/white" />
      <!-- 设置矩形的四个角为弧形 -->
      <!-- android:radius 弧形的半径 -->
      <corners android:radius="7dip" />
 </shape>

动画效果(显示出来)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0" 
        android:duration="300"
        />
    <alpha
        android:fromAlpha="0.7"
        android:toAlpha="1.0" 
        android:duration="120"
        />  
</set>

动画效果(消失)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromYDelta="0"
        android:toYDelta="100%p" 
        android:duration="1300"
        />
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.5" 
        android:duration="800"
        />  
</set>

具体功能就看源码吧
源码地址:http://yunpan.cn/cdvQ9D772PnLd 访问密码 2112
今天写的有点累。。。。。唉。。。蛋疼。。。
顺便补充下,天津的消防员辛苦了。。唉。。。

目录
相关文章
|
30天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
20 1
|
2月前
|
物联网 vr&ar Android开发
探索安卓与iOS操作系统的未来发展趋势
【2月更文挑战第9天】本文将深入探讨安卓与iOS操作系统的未来发展趋势。通过分析当前技术发展和市场趋势,我们将探讨移动操作系统在人工智能、虚拟现实、物联网等领域的应用前景,以及如何满足用户需求并提升用户体验。同时,我们还将着重讨论两大操作系统在隐私保护、系统优化和生态建设方面的不断改进。
27 4
|
2月前
|
搜索推荐 Android开发 iOS开发
探析安卓与iOS系统的优劣
【2月更文挑战第7天】安卓与iOS是当今手机市场上最主流的两款操作系统,各有优劣。本文将从用户体验、开放程度、生态系统等方面对两者进行深入探析,以期帮助读者更好地了解它们的特点。
|
28天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS操作系统的发展与比较
在移动互联网时代,安卓和iOS两大操作系统在智能手机市场竞争激烈。本文将从技术架构、生态系统、用户体验等方面对安卓和iOS进行比较分析,探讨它们各自的特点和发展趋势。
|
1月前
|
Web App开发 前端开发 网络安全
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
【2月更文挑战第21天】前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
50 1
前端分析工具之 Charles 录制 Android/IOS 手机的 https 应用
|
1月前
|
人工智能 算法 Android开发
探索未来:Android与iOS在人工智能时代的融合与创新
【2月更文挑战第13天】 在数字化时代的快速发展下,Android与iOS作为两大主流移动操作系统,它们在人工智能(AI)领域的融合与创新已成为推动科技进步的关键力量。本文将从操作系统的核心功能拓展、AI技术的集成应用,以及开发者生态系统的演变三个维度,深入探讨Android和iOS如何在AI时代实现协同发展,以及这一进程对用户体验、应用开发和行业趋势产生的深远影响。通过对比分析和案例研究,我们旨在揭示两大平台在AI驱动下的创新路径,及其对未来科技格局的塑造作用。
|
1月前
|
人工智能 自然语言处理 语音技术
探索未来:安卓与iOS在人工智能领域的竞争与合作
【2月更文挑战第12天】本文深入探讨了安卓和iOS两大操作系统在人工智能(AI)领域的发展现状、竞争态势及未来合作可能性。通过对比分析两系统在AI技术集成、开发者支持、用户体验优化等方面的表现,揭示了它们各自的优势与挑战。文章最终展望了一个既有竞争又充满合作的未来,认为安卓和iOS的共同进步将推动整个人工智能技术向前发展,为用户带来更加智能、便捷的生活体验。
|
1月前
|
搜索推荐 Android开发 iOS开发
探索未来:安卓与iOS双系统的融合与创新
【2月更文挑战第12天】 在数字化时代,智能手机操作系统的发展不仅代表了技术的进步,更是用户体验革新的前沿。本文深入探讨了安卓和iOS这两大主流操作系统的未来走向,特别是它们在技术融合与创新方面的可能性。通过分析当前的市场需求、技术挑战和潜在的发展机会,我们将展望一个可能出现的未来场景:一个结合了安卓开放性和iOS优雅体验的双系统融合平台。这不仅仅是对技术极限的挑战,更是对用户体验极致追求的一次探索。
37 2
|
1月前
|
人工智能 搜索推荐 Android开发
探索未来:安卓与iOS在人工智能时代的融合趋势
【2月更文挑战第12天】 在这篇探索性文章中,我们将深入分析安卓和iOS两大移动操作系统在人工智能(AI)时代的融合趋势。随着技术的飞速发展,AI已成为推动智能手机进化的关键力量。本文通过对安卓和iOS各自在AI领域的最新进展进行比较,揭示了两大平台如何在保持各自特色的同时,也在向着更加智能、更加个性化的方向发展。我们不仅聚焦于当前的技术现状,而且还将展望未来,探讨这一趋势对用户体验、应用开发以及整个科技生态的深远影响。
|
1月前
|
人工智能 安全 搜索推荐
探索未来:安卓与iOS在人工智能时代的融合与创新
【2月更文挑战第12天】随着人工智能(AI)技术的飞速发展,安卓和iOS系统作为智能手机操作系统的两大巨头,正面临着前所未有的融合与创新机遇。本文将从AI技术对移动操作系统的影响、两大系统在AI领域的创新应用,以及未来可能的融合方向三个方面进行深入探讨。通过对比分析,我们旨在揭示安卓和iOS在人工智能时代如何共同推进技术革新,为用户提供更加智能、便捷的服务体验。