代码如下
package com.yq1012.youku; import android.support.v4.app.Fragment; import android.transition.ChangeBounds; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; import android.os.Build; public class MainActivity extends Activity implements OnClickListener { private boolean isLever2Show = true; private boolean isLever3Show = true; private boolean isLever1Show = true; private ImageView icon_menu; private ImageView icon_home; private RelativeLayout level1; private RelativeLayout level2; private RelativeLayout level3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); icon_menu = (ImageView) findViewById(R.id.iv_menu); icon_home = (ImageView) findViewById(R.id.iv_home); level1 = (RelativeLayout) findViewById(R.id.rl_level1); level2 = (RelativeLayout) findViewById(R.id.rl_level2); level3 = (RelativeLayout) findViewById(R.id.rl_level3); icon_menu.setOnClickListener(this); icon_home.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.iv_menu: if (isLever3Show) { // 如果是 3级菜单是显示 就隐藏 MyUtil.startAnimOut(level3); } else { // 隐藏 。。 MyUtil.startAnimIn(level3); } isLever3Show = !isLever3Show; break; case R.id.iv_home: if (isLever2Show) { // 如果是 3级菜单是显示 就隐藏 MyUtil.startAnimOut(level2); isLever2Show = false; if (isLever3Show) {// 存在的第3级也在将其隐藏 MyUtil.startAnimOut(level3, 200); isLever3Show = false; } } else { // 隐藏 。。 MyUtil.startAnimIn(level2); isLever2Show = true; } break; default: break; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU) {// 监听menu按键 chageLevel1State(); } return super.onKeyDown(keyCode, event); } /** * */ private void chageLevel1State() { if (isLever1Show) { MyUtil.startAnimOut(level1); isLever1Show = false; if (isLever2Show) { MyUtil.startAnimOut(level2,200); isLever2Show = false; if (isLever3Show) { MyUtil.startAnimOut(level3,100); isLever3Show = false; } } }else{ MyUtil.startAnimIn(level1); isLever1Show = true; MyUtil.startAnimIn(level2,200); isLever2Show = true; } } }
界面如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" 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.yq1012.youku.MainActivity$PlaceholderFragment" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> <RelativeLayout android:id="@+id/rl_level1" android:layout_width="90dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/level1" android:gravity="center" > <ImageView android:id="@+id/iv_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_home" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_level2" android:layout_width="180dp" android:layout_height="90dp" android:layout_alignBottom="@+id/rl_level1" android:layout_centerHorizontal="true" android:background="@drawable/level2" > <ImageView android:id="@+id/iv_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="10dp" android:background="@drawable/icon_search" /> <ImageView android:id="@+id/iv_menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:background="@drawable/icon_menu" /> <ImageView android:id="@+id/iv_myyouku" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="10dp" android:background="@drawable/icon_myyouku" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_level3" android:layout_width="280dp" android:layout_height="140dp" android:layout_alignBottom="@+id/rl_level2" android:layout_centerHorizontal="true" android:background="@drawable/level3" > <ImageView android:id="@+id/channel1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:background="@drawable/channel1" /> <ImageView android:id="@+id/channel2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/channel1" android:layout_alignLeft="@id/channel1" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:background="@drawable/channel2" /> <ImageView android:id="@+id/channel4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:src="@drawable/channel4" /> <ImageView android:id="@+id/channel7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="10dp" android:layout_marginRight="10dp" android:background="@drawable/channel7" /> <ImageView android:id="@+id/channel6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/channel7" android:layout_alignRight="@id/channel7" android:layout_marginBottom="10dp" android:layout_marginRight="20dp" android:background="@drawable/channel6" /> <ImageView android:id="@+id/channel5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/channel6" android:layout_toLeftOf="@+id/channel6" android:background="@drawable/channel5" /> <ImageView android:id="@+id/channel3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/channel5" android:layout_toRightOf="@+id/channel2" android:background="@drawable/channel3" /> </RelativeLayout> </RelativeLayout>
这个界面很适合熟练 RelativeLayout