一、自定義控件

01.通過組合已有控件實現(xiàn)特殊效果----優(yōu)酷菜單

效果圖

youku.gif

代碼
MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView icon_home;
    private ImageView icon_menu;
    private RelativeLayout level1;
    private RelativeLayout level2;
    private RelativeLayout level3;

    /**
     * 是否顯示level1
     * true 顯示
     * false 隱藏
     */
    private boolean isShowLevel1 = true;

    /**
     * 是否顯示level2
     * true 顯示
     * false 隱藏
     */
    private boolean isShowLevel2 = true;

    /**
     * 是否顯示level3
     * true 顯示
     * false 隱藏
     */
    private boolean isShowLevel3 = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        icon_home = (ImageView) findViewById(R.id.icon_home);
        icon_menu = (ImageView) findViewById(R.id.icon_menu);
        level1 = (RelativeLayout) findViewById(R.id.level1);
        level2 = (RelativeLayout) findViewById(R.id.level2);
        level3 = (RelativeLayout) findViewById(R.id.level3);

        //設(shè)置點擊事件
        MyOnClickListener listener = new MyOnClickListener();
        icon_home.setOnClickListener(listener);
        icon_menu.setOnClickListener(listener);


    }

    class MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.icon_home:
                    if (isShowLevel3 && isShowLevel2){
                        Tools.hideView(level3,300);
                        Tools.hideView(level2);
                        isShowLevel3 = false;
                        isShowLevel2 = false;
                    }else{
                        Tools.showView(level2);
                        isShowLevel2 = true;
                    }
                    break;

                case R.id.icon_menu:
                    if (isShowLevel3){
                        //隱藏
                        Tools.hideView(level3);
                        isShowLevel3 = false;
                    }else{
                        //顯示
                        Tools.showView(level3);
                        isShowLevel3 = true;
                    }
                    break;
            }
        }
    }
}

布局文件
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/level3"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/level3"
            android:layout_width="280dp"
            android:layout_height="140dp">

            <ImageView
                android:id="@+id/channel1"
                android:layout_marginLeft="8dp"
                android:layout_marginBottom="8dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/channel1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_above="@id/channel1"
                android:id="@+id/channel2"
                android:layout_marginLeft="24dp"
                android:src="@drawable/channel2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_above="@id/channel2"
                android:id="@+id/channel3"
                android:layout_marginLeft="48dp"
                android:src="@drawable/channel3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_marginTop="7dp"
                android:layout_centerHorizontal="true"
                android:id="@+id/channel4"
                android:src="@drawable/channel4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_alignParentRight="true"
                android:id="@+id/channel7"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/channel7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_marginRight="24dp"
                android:layout_alignParentRight="true"
                android:layout_above="@id/channel7"
                android:id="@+id/channel6"
                android:src="@drawable/channel6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_marginRight="48dp"
                android:layout_alignParentRight="true"
                android:layout_above="@id/channel6"
                android:id="@+id/channel5"
                android:src="@drawable/channel5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/level2"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/level2"
            android:layout_width="180dp"
            android:layout_height="90dp">

            <ImageView
                android:layout_marginLeft="8dp"
                android:layout_marginBottom="8dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/icon_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/icon_menu"
                android:layout_marginTop="5dp"
                android:layout_centerHorizontal="true"
                android:src="@drawable/icon_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ImageView
                android:layout_alignParentRight="true"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="8dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/icon_myyouku"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>



        <RelativeLayout
            android:id="@+id/level1"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/level1"
            android:layout_width="100dp"
            android:layout_height="50dp">

            <ImageView
                android:id="@+id/icon_home"
                android:layout_centerInParent="true"
                android:src="@drawable/icon_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>

    </RelativeLayout>


</android.support.constraint.ConstraintLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末哲身,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖朴摊,帶你破解...
    沈念sama閱讀 219,270評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異此虑,居然都是意外死亡甚纲,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評論 3 395
  • 文/潘曉璐 我一進店門朦前,熙熙樓的掌柜王于貴愁眉苦臉地迎上來介杆,“玉大人,你說我怎么就攤上這事韭寸〈荷冢” “怎么了?”我有些...
    開封第一講書人閱讀 165,630評論 0 356
  • 文/不壞的土叔 我叫張陵恩伺,是天一觀的道長赴背。 經(jīng)常有香客問我,道長莫其,這世上最難降的妖魔是什么癞尚? 我笑而不...
    開封第一講書人閱讀 58,906評論 1 295
  • 正文 為了忘掉前任耸三,我火速辦了婚禮乱陡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘仪壮。我一直安慰自己憨颠,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,928評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著爽彤,像睡著了一般养盗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上适篙,一...
    開封第一講書人閱讀 51,718評論 1 305
  • 那天往核,我揣著相機與錄音,去河邊找鬼嚷节。 笑死聂儒,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的硫痰。 我是一名探鬼主播衩婚,決...
    沈念sama閱讀 40,442評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼效斑!你這毒婦竟也來了非春?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,345評論 0 276
  • 序言:老撾萬榮一對情侶失蹤缓屠,失蹤者是張志新(化名)和其女友劉穎奇昙,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體敌完,經(jīng)...
    沈念sama閱讀 45,802評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡敬矩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,984評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蠢挡。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弧岳。...
    茶點故事閱讀 40,117評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖业踏,靈堂內(nèi)的尸體忽然破棺而出禽炬,到底是詐尸還是另有隱情,我是刑警寧澤勤家,帶...
    沈念sama閱讀 35,810評論 5 346
  • 正文 年R本政府宣布腹尖,位于F島的核電站,受9級特大地震影響伐脖,放射性物質(zhì)發(fā)生泄漏热幔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,462評論 3 331
  • 文/蒙蒙 一讼庇、第九天 我趴在偏房一處隱蔽的房頂上張望绎巨。 院中可真熱鬧,春花似錦蠕啄、人聲如沸场勤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽和媳。三九已至格遭,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間留瞳,已是汗流浹背拒迅。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留她倘,地道東北人坪它。 一個月前我還...
    沈念sama閱讀 48,377評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像帝牡,于是被迫代替她去往敵國和親往毡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,060評論 2 355