Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法
本文實(shí)例講述了Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法。分享給大家供大家參考,具體如下:
這里使用activity實(shí)現(xiàn)彈出滑動(dòng)窗口或菜單,主要是使用了一些設(shè)置activity的樣式來(lái)實(shí)現(xiàn)彈出窗口和滑動(dòng)效果,實(shí)現(xiàn)如下:
第一步:設(shè)計(jì)要彈出窗口的xml布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" android:layout_alignParentBottom="true" android:background="@drawable/btn_style_alert_dialog_background" > <Button android:id="@+id/btn_take_photo" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> <Button android:id="@+id/btn_pick_photo" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="5dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="從相冊(cè)選擇" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> <Button android:id="@+id/btn_cancel" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="15dip" android:layout_marginBottom="15dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消" android:background="@drawable/btn_style_alert_dialog_cancel" android:textColor="#ffffff" android:textStyle="bold" /> </LinearLayout> </RelativeLayout>
第二步:創(chuàng)建SelectPicPopupWindow類繼承Activity類并實(shí)現(xiàn)OnClickListener接口(可以不用在這里實(shí)現(xiàn)這個(gè)借口,根據(jù)自己需要和方便實(shí)現(xiàn)),其他代碼實(shí)現(xiàn)跟編寫常規(guī)Activity一樣就OK,如下:
import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; public class SelectPicPopupWindow extends Activity implements OnClickListener{ private Button btn_take_photo, btn_pick_photo, btn_cancel; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alert_dialog); btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo); btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo); btn_cancel = (Button) this.findViewById(R.id.btn_cancel); layout=(LinearLayout)findViewById(R.id.pop_layout); //添加選擇窗口范圍監(jiān)聽(tīng)可以優(yōu)先獲取觸點(diǎn),即不再執(zhí)行onTouchEvent()函數(shù),點(diǎn)擊其他地方時(shí)執(zhí)行onTouchEvent()函數(shù)銷毀Activity layout.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "提示:點(diǎn)擊窗口外部關(guān)閉窗口!", Toast.LENGTH_SHORT).show(); } }); //添加按鈕監(jiān)聽(tīng) btn_cancel.setOnClickListener(this); btn_pick_photo.setOnClickListener(this); btn_take_photo.setOnClickListener(this); } //實(shí)現(xiàn)onTouchEvent觸屏函數(shù)但點(diǎn)擊屏幕時(shí)銷毀本Activity @Override public boolean onTouchEvent(MotionEvent event){ finish(); return true; } public void onClick(View v) { switch (v.getId()) { case R.id.btn_take_photo: break; case R.id.btn_pick_photo: break; case R.id.btn_cancel: break; default: break; } finish(); } }
第三步:編寫MainActivity類,這里很簡(jiǎn)單就是點(diǎn)擊啟動(dòng)剛才要實(shí)現(xiàn)窗口的MainActivity即可:
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) this.findViewById(R.id.text); //把文字控件添加監(jiān)聽(tīng),點(diǎn)擊彈出自定義窗口 tv.setOnClickListener(new OnClickListener() { public void onClick(View v) { startActivity(new Intent(MainActivity.this,SelectPicPopupWindow.class)); } }); } }
第四步:這里要注意下AndroidManifest.xml對(duì)SelectPicPopupWindow的配置跟常規(guī)的不一樣為該activity改添加android:theme屬性,如下:
第五步:這一步是實(shí)現(xiàn)本實(shí)例最重要的一部就是設(shè)置android:theme屬性樣式以實(shí)現(xiàn)本例所需要的效果,如下:
<style name="AnimBottom" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/push_bottom_in</item> <item name="android:windowExitAnimation">@anim/push_bottom_out</item> </style> <style name="MyDialogStyleBottom" parent="android:Theme.Dialog"> <item name="android:windowAnimationStyle">@style/AnimBottom</item> <item name="android:windowFrame">@null</item> ;!-- 邊框 --> <item name="android:windowIsFloating">true</item> ;!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsTranslucent">true</item> ;!-- 半透明 --> <item name="android:windowNoTitle">true</item> ;!-- 無(wú)標(biāo)題 --> <item name="android:windowBackground">@android:color/transparent</item> ;!-- 背景透明 --> <item name="android:backgroundDimEnabled">true</item> ;!-- 模糊 --> </style>
第六步:在貼出彈出和銷毀時(shí)的動(dòng)畫效果代碼:
push_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑入式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> </set>
push_buttom_out.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑出式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> </set>
注意:這兩個(gè)xml需要放在res/anim的anim文件夾下
第七步;運(yùn)行效果如圖:
安卓4.0的手機(jī)測(cè)試時(shí)滑出的的窗口可能與整個(gè)屏幕兩側(cè)會(huì)有間隙
如果要讓此界面的寬度撐滿整個(gè)屏幕,可以在
setContentView(R.layout.activity_main);
之后加上
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 淺談 Android 7.0 多窗口分屏模式的實(shí)現(xiàn)
- Android Studio升級(jí)3.6 Build窗口出現(xiàn)中文亂碼問(wèn)題解決方法
- Android開發(fā)實(shí)現(xiàn)模仿微信小窗口功能【Dialog對(duì)話框風(fēng)格窗口】
- Android監(jiān)聽(tīng)系統(tǒng)來(lái)電并彈出提示窗口
- Android開發(fā)實(shí)現(xiàn)popupWindow彈出窗口自定義布局與位置控制方法
- Android Popupwindow彈出窗口的簡(jiǎn)單使用方法
- Android分屏多窗口的實(shí)踐代碼
- Android自定義彈出窗口PopupWindow使用技巧
- Android窗口小部件基礎(chǔ)編寫代碼實(shí)例
相關(guān)文章
Android 靜默方式實(shí)現(xiàn)批量安裝卸載應(yīng)用程序的深入分析
本篇文章是對(duì)Android 靜默方式實(shí)現(xiàn)批量安裝卸載應(yīng)用程序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android中的ViewPager視圖滑動(dòng)切換類的入門實(shí)例教程
Android中ViewPager通常與Fragments組件共同使用來(lái)實(shí)現(xiàn)視圖切換功能,本文就帶大家一起來(lái)學(xué)習(xí)Android中的ViewPager視圖滑動(dòng)切換類的入門實(shí)例教程:2016-06-06Android計(jì)步功能的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android計(jì)步功能的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Android studio gradle環(huán)境變量配置教程
這篇文章主要為大家詳細(xì)介紹了Android studio gradle環(huán)境變量配置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離案例講解
這篇文章主要介紹了Android中ScrollView監(jiān)聽(tīng)滑動(dòng)距離案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android 開機(jī)自啟動(dòng)Service實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android 開機(jī)自啟動(dòng)Service實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android開發(fā)筆記之:如何屏蔽Button setClickable與setEnabled
本篇文章是對(duì)在Android中,如何屏蔽Button setClickable與setEnabled的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05