Android仿微信實(shí)現(xiàn)下拉列表
本文要實(shí)現(xiàn)微信6.1中點(diǎn)擊頂部菜單欄的“+”號(hào)按鈕時(shí),會(huì)彈出一個(gè)列表框。這里用的了Activity實(shí)現(xiàn),其實(shí)最好的方法可以用ActionBar,不過這貨好像只支持3.0以后的版本。本文的接上文Android仿微信底部菜單欄+頂部菜單欄。
效果
一、仿微信下拉列表布局pop_dialog.xml
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="45dp" android:layout_marginRight="20dp"> <LinearLayout android:id="@+id/id_pop_dialog_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@drawable/pop_item_normal" android:orientation="vertical" > <LinearLayout android:id="@+id/id_groupchat" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:background="@drawable/pop_list_selector" > <ImageView android:id="@+id/id_imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/pop_group" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="發(fā)起聊天" android:layout_gravity="center_vertical" android:textColor="#fff" android:textSize="16sp" /> </LinearLayout> <ImageView android:id="@+id/id_imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pop_line" /> <LinearLayout android:id="@+id/id_addfrd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/pop_list_selector" > <ImageView android:id="@+id/id_imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/pop_add" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="添加朋友" android:layout_gravity="center_vertical" android:textColor="#fff" android:textSize="16sp" /> </LinearLayout> <ImageView android:id="@+id/id_imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pop_line" /> <LinearLayout android:id="@+id/id_find" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/pop_list_selector" > <ImageView android:id="@+id/id_imageView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/pop_qrcode" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="掃一掃" android:layout_gravity="center_vertical" android:textColor="#fff" android:textSize="16sp" /> </LinearLayout> <ImageView android:id="@+id/id_imageView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/pop_line" /> <LinearLayout android:id="@+id/id_feedback" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="3dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/pop_list_selector" > <ImageView android:id="@+id/id_imageView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="8dp" android:src="@drawable/pop_feedback" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="幫助與反饋" android:layout_gravity="center_vertical" android:textColor="#fff" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout>
其中,按下圖片后變換顏色:
android:background="@drawable/pop_list_selector" >
pop_list_selector.xml如下
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/pop_item_pressed" android:state_focused="true"/> <item android:drawable="@drawable/pop_item_pressed" android:state_pressed="true"/> <item android:drawable="@drawable/pop_item_pressed" android:state_selected="true"/> <item android:drawable="@drawable/pop_item_normal"/> </selector>
看看效果,這是去掉標(biāo)題欄后的(也可以用代碼去掉)
去掉標(biāo)題欄的方法:
二、對(duì)應(yīng)代碼
pop_dialog.xml對(duì)應(yīng)的代碼為PopDialogActivity.java
如下:
package com.example.tabexample; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.widget.LinearLayout; public class PopDialogActivity extends Activity implements OnClickListener{ //定義四個(gè)按鈕區(qū)域 private LinearLayout mGroupChat; private LinearLayout mAddFrd; private LinearLayout mFind; private LinearLayout mFeedBack; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.pop_dialog); initView(); } /** * 初始化組件 */ private void initView(){ //得到布局組件對(duì)象并設(shè)置監(jiān)聽事件 mGroupChat = (LinearLayout)findViewById(R.id.id_groupchat); mAddFrd = (LinearLayout)findViewById(R.id.id_addfrd); mFind = (LinearLayout)findViewById(R.id.id_find); mFeedBack = (LinearLayout)findViewById(R.id.id_feedback); mGroupChat.setOnClickListener(this); mAddFrd.setOnClickListener(this); mFind.setOnClickListener(this); mFeedBack.setOnClickListener(this); } @Override public boolean onTouchEvent(MotionEvent event){ finish(); return true; } @Override public void onClick(View v) { } }
三、設(shè)置背景透明
如果單這樣,當(dāng)這個(gè)Activity出來后,就會(huì)把之前的Activity覆蓋,但是如果把它背景設(shè)置成透明的不就可以了么?方法如下:
在AndroidManifest.xml中添加:
<!-- 這里一定要注冊(cè)上這個(gè)activity,否則跳轉(zhuǎn)將會(huì)失敗,因?yàn)橄到y(tǒng)找不到這個(gè)activity --> t;activity android:name="com.example.tabexample.PopDialogActivity" android:label="@string/app_name" android:theme="@style/MyDialogStyleTop"> t;/activity>
其中
"@style/MyDialogStyleTop"
是我自己定義的格式,在value/style下添加:
<style name="MyDialogStyleTop" parent="android:Theme.Dialog"> <item name="android:windowFrame">@null</item><!-- 邊框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsTranslucent">false</item><!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 無標(biāo)題 --> <item name="android:windowBackground">@android:color/transparent</item><!-- 背景透明 --> <item name="android:backgroundDimEnabled">false</item><!-- 模糊 --> </style>
四、使用
其實(shí)使用就是Activity的跳轉(zhuǎn)了,方法很簡(jiǎn)單,一句:
startActivity(new Intent(MainActivity.this,PopDialogActivity.class));
把這句放在“+”按鈕的點(diǎn)擊事件當(dāng)中去,這里添加點(diǎn)擊事件就不用說了,很簡(jiǎn)單,然后最終的效果如下:
本文已被整理到了《Android微信開發(fā)教程匯總》,歡迎大家學(xué)習(xí)閱讀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- Android UI組件Spinner下拉列表詳解
- Android下拉列表選項(xiàng)框及指示箭頭動(dòng)畫
- Android自定義Spinner下拉列表(使用ArrayAdapter和自定義Adapter實(shí)現(xiàn))
- Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽功能
- Android自定義單選多選下拉列表的實(shí)例代碼
- Android仿美團(tuán)淘寶實(shí)現(xiàn)多級(jí)下拉列表菜單功能
- Android使用Spinner控件實(shí)現(xiàn)下拉列表的案例
- Android下拉列表spinner的實(shí)例代碼
- Android Studio實(shí)現(xiàn)下拉列表效果
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法,涉及Android界面布局與相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2018-01-01Android studio實(shí)現(xiàn)菜單效果
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)菜單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android自定義控件案例匯總1(菜單、popupwindow、viewpager)
這篇文章主要介紹了Android自定義控件案例匯總,優(yōu)酷菜單、popupwindow實(shí)現(xiàn)下拉列表、viewpager實(shí)現(xiàn)輪播圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android實(shí)現(xiàn)瘋狂連連看游戲之加載界面圖片和實(shí)現(xiàn)游戲Activity(四)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之加載界面圖片和實(shí)現(xiàn)游戲Activity,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03android開機(jī)自動(dòng)啟動(dòng)app的解決方法
這篇文章主要為大家詳細(xì)介紹了android開機(jī)自動(dòng)啟動(dòng)app的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下2017-08-08android連續(xù)拖動(dòng)導(dǎo)致掛起的解決方法
本文給大家分享的是在安卓的項(xiàng)目開發(fā)中遇到連續(xù)拖動(dòng)對(duì)象,導(dǎo)致掛起的問題的解決方法,也是經(jīng)過很多網(wǎng)友的提示,最終才找到解決方法,這里記錄一下,分享給大家。2015-05-05Android如何實(shí)現(xiàn)非本地圖片的點(diǎn)擊態(tài)
Android如何實(shí)現(xiàn)非本地圖片的點(diǎn)擊態(tài),本文提供了詳細(xì)的實(shí)現(xiàn)代碼,需要了解的朋友可以參考下2012-12-12Android中RecyclerView實(shí)現(xiàn)分頁滾動(dòng)的方法詳解
RecyclerView實(shí)現(xiàn)滾動(dòng)相信對(duì)大家來說都不陌生,但是本文主要給大家介紹了利用Android中RecyclerView實(shí)現(xiàn)分頁滾動(dòng)的思路和方法,可以實(shí)現(xiàn)翻頁功能,一次翻一頁,也可以實(shí)現(xiàn)翻至某一頁功能。文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04BootStrapValidator與My97日期校驗(yàn)的實(shí)例代碼
這篇文章給大家介紹了bootstrapvalidator與my97日期校驗(yàn)的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01