1. 程式人生 > >用popupwindow和selector實現仿qq的下拉選單

用popupwindow和selector實現仿qq的下拉選單

概要

做了幾天紙模,所以沒來得及更新部落格~生活才是主流嘛,嘿嘿,小展示一下我的成果

哈哈~言歸正傳,這次我們來看一下如何實現qq裡面的下拉列表(我們的比他漂亮哦~)

效果展示

我們要實現的
我們的效果

知識點

1.什麼是popupwindow? 答:我們做的就是 2.什麼是selector? 答:我們這裡的selector主要是實現點選時和點選後控制元件呈現不同的動畫效果 3.如何給LinearLayout設定selector,又怎麼給它裡面的textview設定同步效果?  答:程式碼裡有解釋 4.如何正確的設定popupwindow的位置?答: popupwindow的showasdropdown方法是預設左對齊的,具體如何讓他居中請看程式碼

DEMO

我們要完成這個首先要完成這些東西:主介面、標題欄、popupwindow介面、linearLayout的selector點選效果(這裡錄得不明顯,其實他是有一個變色效果的)、Textview的selector點選效果、主Activity的邏輯 主介面、標題欄就不再贅述了,首先看一下popupwindow的介面 就是一個垂直分佈的linearlayout裡面放了四個水平分佈的linearlayout,每個水平的裡面有一個textview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_pop">
    <LinearLayout

        android:layout_marginTop="8dp"
        android:id="@+id/all_customer"
        android:layout_width="fill_parent"
        android:layout_height="28dp"
        android:orientation="horizontal"
        android:background="@drawable/selector_lieanerlayout">
        <TextView
            android:duplicateParentState="true"

這裡設定

android:duplicateParentState="true"

的意思是使他可以接受他所在的linearLayout的點選事件,這樣當我們點選LinearLayout時,也會觸發他的selector,使他跟著變顏色


            android:layout_marginLeft="8dp"
            android:id="@+id/tv_all_customer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全部"
            android:textSize="16dp"
            android:textColor="@color/selector_text"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/recommend"
        android:layout_width="fill_parent"
        android:layout_height="28dp"
        android:orientation="horizontal"
        android:background="@drawable/selector_lieanerlayout"

這裡的背景就是我們設定的selector

>
        <TextView
            android:duplicateParentState="true"
            android:layout_marginLeft="8dp"
            android:id="@+id/tv_recommend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="推薦"
            android:textSize="16dp"
            android:textColor="@color/selector_text"

這個也是我們自己設定的selector

/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/appointment"
        android:layout_width="fill_parent"
        android:layout_height="28dp"
        android:orientation="horizontal"
        android:background="@drawable/selector_lieanerlayout">
        <TextView
            android:duplicateParentState="true"
            android:layout_marginLeft="8dp"
            android:id="@+id/tv_appointment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="預約"
            android:textSize="16dp"
            android:textColor="@color/selector_text"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/customer_entering"
        android:layout_width="fill_parent"
        android:layout_height="28dp"
        android:orientation="horizontal"
        android:background="@drawable/selector_lieanerlayout">
        <TextView
            android:duplicateParentState="true"
            android:layout_marginLeft="8dp"
            android:id="@+id/tv_customer_entering"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="案場錄入"
            android:textSize="16dp"
            android:textColor="@color/selector_text"/>

    </LinearLayout>


</LinearLayout>
佈局比較簡單,只有一個地方需要注意的~ 下面是兩個selector LinearLayout的佈局檔案放到drawable裡(這個資料夾裡的xml都要指定drawable屬性,一般用來做背景切換的),所以我們認識到文字顏色不能放到這個資料夾,因為文字的顏色不是背景,如果這麼用了只能設定文字背景,所以我們在res下新建一個color資料夾(color型別的) 然後在這個資料夾裡放入更改文字的selector 下面上程式碼 linearLayout的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@color/popupwindow1"/>
    <item android:state_enabled="true"
        android:drawable="@color/popupwindow2"/>

</selector>

這裡說明一下,我們在res->value下有一個colors檔案,用來統一管理color的,有時候一個專案需要使用一個顏色很多次,我們這樣可以更方便的管理(如果你的ide沒有可以自己建一個)
至於selector裡的程式碼什麼意思大家肯定一看就明白~全是英文╮(╯_╰)╭ 繼續,textview的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/orange" />
    <item android:state_focused="true" android:color="@color/orange" />
    <item android:state_pressed="true" android:color="@color/orange" />
    <item android:color="#ffffff" />

</selector>

這裡面用的是android:color 而不是drawable,因為前者是顏色,後者是圖片,文字設定的是顏色,不是圖片~ 繼續,我們的前期工作都準備好了,就差最後一步了!! 首先要使用popupwindow我們就要知道他是什麼,他是一個阻塞程序的對話方塊,也就是說正常情況下只要他在除了home鍵其他的都用不了,設定他的方法是這樣
View root = this.getLayoutInflater().inflate(R.layout.poppupwin_my_client,null);
        popup = new PopupWindow(root,450,360);
        popup.setFocusable(true);
第一句是用來找到我們的popupwindow的介面,第二句是給它能獲取焦點(就是能被點選)的權利 我們之前不是說過它可以阻塞程序嗎?為什麼我們的示例中點選別的地方它會消失??只要加上這兩句即可
        popup.setOutsideTouchable(true);
        popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.custom_pop));
第一句是讓我們可以點選外面的地方,第二句是給他畫背景,缺一不可(我們的背景就是佈局檔案裡的背景,畫個一模一樣的就ok) 最後我們給標題欄裡的按鈕設定監聽,當點選時
     popup.showAsDropDown(tittle,-tittle.getWidth()/2,4);
tittle是我們標題欄裡的一個listview“我的客戶”,我們之前說過是左對齊,這句話第一個變數是一個控制元件,popupwindow在這個控制元件下拉開,第二個和第三個是x、y軸方向的偏移量,左負右正,上負下正 這樣就完成了,最後記得給我們的Linearlayout加上監聽,這樣才能使其有效果
allCustom = (LinearLayout)root.findViewById(R.id.all_customer);
        recommend =(LinearLayout)root.findViewById(R.id.recommend);
        appointment=(LinearLayout)root.findViewById(R.id.appointment);
        customEntering=(LinearLayout)root.findViewById(R.id.customer_entering);
        allCustom.setOnClickListener(this);
        recommend.setOnClickListener(this);
        appointment.setOnClickListener(this);
        customEntering.setOnClickListener(this);


感想

   作為一個程式設計師的我們真的要學會生活,人這一輩子最悲傷的事就是:“人死了錢沒花了~”哈哈,程式設計師們!快樂的活著吧!! 每日一句: 他/她一定會來,你要等。