1. 程式人生 > >Android PopupWindow實現,類似於iOS的選擇欄

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
今天寫的有點累。。。。。唉。。。蛋疼。。。
順便補充下,天津的消防員辛苦了。。唉。。。

相關推薦

Android PopupWindow實現類似iOS選擇

以前專案中也做過類似的東西,今天想到就寫一篇關於這樣的東西了,外面這一類的實現也很多,都可以借鑑,但是,謝了也就寫了,嗯!! 專案結構: 執行效果 完成了佈局的圓角,彈出彈出的動畫以及一系列監聽事件 主Activity public cla

android實現本地視訊的播放類似一個小型的MP4可以選擇本地的檔案進行播放

首先呢我們來 看一下佈局檔案中的程式碼: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android實現背景透明度漸變的效果類似分享底部的彈出框

實現背景透明度漸變的效果,類似於分享底部的彈出框 這是公司最近寫的一個專案,其中的一個自定義分享模組,剛開始實現的效果比較簡單,背景透明度沒有漸變,而是隨著底部分享的彈出框而一起彈出,看起來比較生硬,使用者體驗不是很好,現在要實現的效果就是類似於SharedS

Android ListView和Fragment結合使用類似某電商的實現拿來就能用,詳細標註適合新手

一個類似於某電商的實現,讓菜鳥們理解Activity與Fragment之間的引數是如何互動的。 包結構: 執行後的效果 分析: 左側ListView可上下拖動,點選不同的item會影響右側Fragment的內容。 廢話不多說,上程式碼(

Android -- 固定在ScrollView頂部的View類似新浪微博的評論列表的頂部

現在很多App都實現了這個功能,例如新浪微博評論頁面的評論、轉發、讚的數字可以固定在螢幕上方。我個人很喜歡這種設計,所以利用一點空餘時間簡單實現了一個類似的功能。 先來看一下上面這張圖的效果。 這個是新浪微博的一個頁面,整體佈局大致分了三塊:正文內容、轉發評論贊的

Android類似iOS相機滑動切換的View

專案地址: https://github.com/duxingzhe/ios-camera-scrollerview-in-android 蘋果相機有一個功能就是左右切換拍攝模式,左右滑動就可以切換

ubuntu 實現開機自動執行類似Windows開機自啟動

有時我們想要在開機的時候,自動執行某些命令,在ubuntu中,很容易辦到: 只需在  /etc/rc.local中加入自己的命令即可。 搜尋關鍵字可知,/etc/init.d/rc.local會呼叫到

Android中Application類用法(整個程式的全域性變數即單例)類似session

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.test"

java實現桌面右下角彈窗(模仿類似qq訊息彈窗)

最近需要一個java實現桌面彈窗的小功能,類似於電腦桌面右下角的小廣告一樣的功能,在csdn上找到一個很好的一個,功能很多,我去除了一點不需要的程式碼,改了下外觀等。 原作者:https://www.cnblogs.com/hgxbo/p/5508384.html 修改後的

微信小程式開發常用技巧(7)——實現一個類似Android toast效果動畫

很多時候,我們在小程式中使用wx.showToast(),發現樣式不是很好看,那麼我們能不能做一個跟原生APP類似的toast呢?答案是肯定的。今天就利用微信小程式的動畫wx.createAnimation()實現一個簡單的類似原生APP的toast提示。 先

Android】側滑選單功能的實現類似QQ)

    通過NavigationView+DrawerLayout來實現側滑選單的功能(效果圖如上),可通過點選左上角圖示以及向右滑動實現側滑選單     首先在主頁佈局xml中使用DreawerLayout作為外包裝,將側滑選單包裝起來即可,而功能則使用navigat

iOS 圖片部分模糊類似美圖秀秀

else false ati only ash created int 使用 last 代碼地址如下:http://www.demodashi.com/demo/14277.html 演示效果 演示效果 代碼結構 項目結構截圖如下: 該模塊的核心源碼部分為

淺析在QtWidget中自定義Model(beginInsertRows()和endInsertRows()是空架子類似一種信號用來通知底層)

cti ron 初學者 開發 http 沒有 insert ati 學習 Qt 4推出了一組新的item view類,它們使用model/view結構來管理數據與表示層的關系。這種結構帶來的功能上的分離給了開發人員更大的彈性來定制數據項的表示,它也提供一個標準的model接

mysql下分組取關聯表指定提示方法類似mssql中的cross apply

nbsp cts ont font ack you 方法 sta lease 轉至:https://stackoverflow.com/questions/12113699/get-top-n-records-for-each-group-of-grouped-result

【kotlin】基本語法when的使用類似java中的switch,但是又青出於藍而勝

href .com log 事情 IT 使用 基本語法 kotlin 參數 when(要判斷的參數){   參數值為1  ->做這種事情   參數值為2  ->做另一種事情   else  ->  類似於switch中的default } 擴展使用:ht

leetcode676+修改一個字母在vector的dict中有沒有類似字典樹暴力

https://leetcode.com/problems/implement-magic-dictionary/description/ class MagicDictionary { public: set<string> s; /** Initialize

科大訊飛 線上語音識別 音訊來源為【檔案】的java接入實現 適用初學者

****科大訊飛的語音識別提供了兩種音訊來源方式,一個是通過麥克風,一個是來自音訊檔案。這裡介紹本人自己寫的通過音訊 檔案識別的java程式碼。**** 【離線識別參考我的另一篇】用java呼叫科大訊飛的離線語音識別dll實現離線識別(JNA實現) 之前的註冊、獲得註冊碼、

用winform實現類似WPF中PopUp控制元件的一段程式碼

用winform實現的類似於WPF中PopUp控制元件的一段程式碼 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using Syste

【策略模式】如何結合spring實現一個介面多個實現如何解決介面選擇問題

1、首先把對映關係放在spring-mvc.xml配置檔案 <bean id="dispatcher" class="com.ms.kai.bms.dispatcher.Abstrac

python中多執行緒開啟的兩種方式(內含有event的應用即安全的機制類似java的等待喚醒機制不會出現多個執行緒之間的錯亂問題)

 event是類似於java中的等待喚醒機制,具體方法參照上一篇CSDN 下面來介紹開啟執行緒的第一種方式 #Filename:threading1.py #開啟執行緒的第一種方式 import threading import time event=threadin