ActivityGroup基本菜单

ActivityGroup基本菜单_第1张图片

 

ActivityGroup可以让多个Activity同时运行在一个屏幕上

 

 

准备图片,它们的名字分别为:picture、menu_exit、menu_main、menu_more、

menu_news、menu_selected、menu_sms、pic_m。

 

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:gravity="center_horizontal"

    android:orientation="vertical">

  <RelativeLayout

      android:layout_width="fill_parent"

      android:layout_height="fill_parent">

      <LinearLayout

          android:id="@+id/content"

          android:orientation="vertical"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent">

      </LinearLayout>

      <GridView

          android:id="@+id/gridviewbar"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"

          android:layout_alignParentBottom="true"

          android:fadingEdgeLength="5dp"

          android:fadingEdge="vertical"/>

  </RelativeLayout>

</LinearLayout>

 

 

 

 

新建布局文件mylayout.xml:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:gravity="center_horizontal"

    android:orientation="vertical">

  <ImageView

      android:layout_marginTop="60dp"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:src="@drawable/picture"/>

</LinearLayout>

 

 

 

 

 

 

 

定义一个BaseAdapter类的子类MenuImageAdapter,以负责所内容的填充操作。

 

package com.li.activitygroupproject;

 

import android.content.Context;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.GridView;

import android.widget.ImageView;

 

public class MenuImageAdapter extends BaseAdapter {

  private ImageView[] menuImg; // 保存所有要填充的组件

  private Context context = null; // 上下文对象

  private int selectedMenuImg; // 选中的索引

 

  public MenuImageAdapter(Context context, int imgIds[], int width,

       int height, int selectedMenuImg) {

     this.context = context;

     this.selectedMenuImg = selectedMenuImg;

     this.menuImg = new ImageView[imgIds.length]; // 开辟新的数组

     for (int x = 0; x < imgIds.length; x++) {

       this.menuImg[x] = new ImageView(this.context); // 实例化ImageView

       this.menuImg[x].setLayoutParams(new GridView.LayoutParams(width,

            height)); // 设置图片的大小

       this.menuImg[x].setAdjustViewBounds(false); // 不调整边界显示

       this.menuImg[x].setPadding(3, 3, 3, 3);// 设置间距

       this.menuImg[x].setImageResource(imgIds[x]);// 设置显示图片

     }

  }

 

  public int getCount() {

     return this.menuImg.length;

  }

 

  public Object getItem(int position) {

     return this.menuImg[position];

  }

 

  public long getItemId(int position) {

     return 0;

  }

 

  public View getView(int position, View convertView, ViewGroup parent) {

     ImageView imgView = null ;

     if(convertView == null) {

       imgView = this.menuImg[position] ;

     } else {

       imgView = (ImageView) convertView ;

     }

     return imgView;

  }

 

  public void setFocus(int selId) {  // 设置选中的显示

     for (int x = 0; x < this.menuImg.length; x++) {

       if (x != selId) {  // 没有选中的

         this.menuImg[x].setBackgroundResource(0) ;  // 不设置背景图片

       }

     }

    this.menuImg[selId].setBackgroundResource(this.selectedMenuImg) ;

  }

}

 

 

 

 

新建一个MyActivity.java类,用于显示图片:

 

package com.li.activitygroupproject;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.MenuItem;

import android.support.v4.app.NavUtils;

 

public class MyActivity extends Activity {

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.mylayout);

    }

}

 

 

 

 

 

 

在MyActivityGroupDemo.java中:

 

package com.li.activitygroupproject;

 

import android.app.ActivityGroup;

import android.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.os.Bundle;

import android.view.Gravity;

import android.view.KeyEvent;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.view.Window;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.GridView;

import android.widget.LinearLayout;

 

public class MyActivityGroupDemo extends ActivityGroup {

  private GridView gridviewToolbar; // 工具菜单栏

  private MenuImageAdapter menu = null; // 图片适配器

  private LinearLayout content = null; // 填充内容

  private int menu_img[] = new int[] { R.drawable.menu_main,

       R.drawable.menu_news, R.drawable.menu_sms, R.drawable.menu_more,

       R.drawable.menu_exit }; // 填充的图片的资源

  private int width = 0; // 求出平均的宽度

  private int height = 0; // 求出平均的高度,定位显示

  private Intent intent = null;

 

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 取消标题

     super.setContentView(R.layout.main);

     this.gridviewToolbar = (GridView) super.findViewById(R.id.gridviewbar);

     this.content = (LinearLayout) super.findViewById(R.id.content);

 

     // 定义工具栏的一些信息显示

     this.gridviewToolbar.setNumColumns(this.menu_img.length); // 求出可以保存的个数

     this.gridviewToolbar.setSelector(new ColorDrawable(Color.TRANSPARENT));

     this.gridviewToolbar.setGravity(Gravity.CENTER);

     this.gridviewToolbar.setVerticalSpacing(0);

 

     this.width = super.getWindowManager().getDefaultDisplay().getWidth()

         / this.menu_img.length;

     this.height = super.getWindowManager().getDefaultDisplay().getHeight() / 8;

 

     this.menu = new MenuImageAdapter(this, this.menu_img, this.width,

         this.height, R.drawable.menu_selected);

     this.gridviewToolbar.setAdapter(this.menu);

     this.switchActivity(0); // 第一个被选中

     this.gridviewToolbar

         .setOnItemClickListener(new OnItemClickListenerImpl());

  }

 

  private class OnItemClickListenerImpl implements OnItemClickListener {

 

     public void onItemClick(AdapterView<?> parent, View view, int position,

         long id) {

       MyActivityGroupDemo.this.switchActivity(position);

     }

  }

 

  private void switchActivity(int id) { // 切换选中的操作

     this.menu.setFocus(id); // 设置选中图片的背景

     this.content.removeAllViews(); // 删除所有的内容

     switch (id) {

     case 0:

       this.intent = new Intent(MyActivityGroupDemo.this, MyActivity.class);

       break;

     case 1:

       this.intent = new Intent(MyActivityGroupDemo.this, MyActivity.class);

       break;

     case 2:

       this.intent = new Intent(MyActivityGroupDemo.this, MyActivity.class);

       break;

     case 3:

       this.intent = new Intent(MyActivityGroupDemo.this, MyActivity.class);

       break;

     case 4:

       this.exitDialog() ;

       return;

     }

     this.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

     Window subActivity = super.getLocalActivityManager().startActivity(

         "subActivity", this.intent);

     this.content.addView(subActivity.getDecorView(),

         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

  }

 

  private void exitDialog() {

     Dialog dialog = new AlertDialog.Builder(this).setIcon(R.drawable.pic_m)

         .setTitle("程序退出? ").setMessage("您确定要退出本程序吗?")

         .setPositiveButton("确定", new DialogInterface.OnClickListener() {

 

           public void onClick(DialogInterface dialog, int which) {

              MyActivityGroupDemo.this.finish() ;

            }

         })

         .setNegativeButton("取消", new DialogInterface.OnClickListener() {

 

           public void onClick(DialogInterface dialog, int which) {

              MyActivityGroupDemo.this.switchActivity(0);

            }

         }).create();

 

     dialog.show();

  }

 

  @Override

  public boolean onKeyDown(int keyCode, KeyEvent event) {

     if(keyCode == KeyEvent.KEYCODE_BACK) {

       this.exitDialog() ;

     }

     return false ;

  }

 

}

 

 

 

 

修改AndroidManifest.xml:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.activitygroupproject"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyActivityGroupDemo"

            android:label="@string/title_activity_my_activity_group_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name=".MyActivity"/>

        <activity android:name=".MenuImageAdapter"/>

    </application>

 

</manifest>

 

 

你可能感兴趣的:(ActivityGroup基本菜单)