初学android,做了个小例子测试menu,结果出现了问题。
代码如下:ImageView.javapackage com.image.action;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;public class ImageView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
     menu.add(0, 1, 0, "enter");
     return super.onCreateOptionsMenu(menu);
    }
    
    public boolean onMenuItemSelected(int featureId,MenuItem item){
    
     switch(item.getItemId()){
     case 1:
     Intent intent = new Intent();
     intent.setClass(this, PicView.class);
     startActivity(intent);
     break;
     }
    
     return super.onMenuItemSelected(featureId, item);
    }
}
PicView.javapackage com.image.action;import java.io.File;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TableRow;public class PicView extends Activity { PopupWindow pwin;

int index = 0;

ImageView view;

File[] sdFiles;

LinearLayout layout;

public void onCreate(Bundle bundle){
super.onCreate(bundle);
File sd_filelist = android.os.Environment.getExternalStorageDirectory();
String sdPath = sd_filelist.getAbsolutePath()+"/";

sdFiles = sd_filelist.listFiles();

layout = new LinearLayout(this);

view = new ImageView(this);
Bitmap bmp = BitmapFactory.decodeFile(sdFiles[index].getAbsolutePath());
Log.v("bitmap", bmp.getWidth()+" : "+ bmp.getHeight());
view.setImageBitmap(bmp);
setContentView(view,new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
if(pwin==null){
pwin = new PopupWindow(layout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT,true);

TableRow row = new TableRow(this);

OnClickListener listener = new Button.OnClickListener(){

public void onClick(View v) {

Log.v("ButtonListener","v:"+v);
Log.v("ButtonListener","this:"+this);

Button b = (Button) v;
String text = (String) b.getText();
Log.v("ButtonText", text);
if(text.equals("←")){
index = (index+sdFiles.length-1)%sdFiles.length;
Log.v("index", index+"");
view.setImageBitmap(BitmapFactory.decodeFile(sdFiles[index].getAbsolutePath()));
}else if(text.equals("→")){
index = (index+1)%sdFiles.length;
view.setImageBitmap(BitmapFactory.decodeFile(sdFiles[index].getAbsolutePath()));
}
}

};


Button view1 = new Button(this);
view1.setText("←");

view1.setOnClickListener(listener);

Button view2 = new Button(this);
view2.setText("■");

view2.setOnClickListener(listener);

Button view3 = new Button(this);
view3.setText("→");

view3.setOnClickListener(listener);

layout.addView(view1);
layout.addView(view2);
layout.addView(view3);

layout.setBackgroundColor(0xFFFFFF);
layout.getBackground().setAlpha(0);
layout.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));



} if(pwin.isShowing()){
pwin.dismiss();
}else{
pwin.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
}
return false;

}

}AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.image.action"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ImageView"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<activity android:name=".PicView" android:label="@string/app_name" />
    </application>
    
     <!-- 创建或者删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 外部存储的修改权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
</manifest>
简单的说:就是先进入ImageView,然后通过菜单中的enter进入PicView,PicView查找sd卡里面所有的
文件(假定SD卡里面全部都是图片文件)。然后把第一张图片显示出来。之后通过Menu里的左右键来切换显示的图片。
现在的问题是:
1.PicView里面的menu菜单全部都挤在左边,而我希望它们能均匀的排列在下面。
2.按menu键之后,dismiss()好象不起作用,菜单不会消失
3.back键失效。无法通过back键回到ImageView

解决方案 »

  1.   

    Back键回不到第一个Activity,是不是ImageView Activity 在已经销毁了?
      

  2.   

    啊?不会吧。我这里看得好好的。要不你直接复制地址看一下?http://i821.photobucket.com/albums/zz134/kgnefe/Strange%20Thing/test.jpg
      

  3.   

    1、你貌似没有对那些图片布局哦,它们肯定都挤在一块了 ,采用GridLayout看下
      

  4.   

    简单的说,就是使用了PopupWindow来取代系统的菜单。
    但是在这个PopupWindow里LinearLayout里面的3个BUTTON都挤在左边。
    而且使用了这个PopupWindow以后,按Back键和Menu键都失效了。
      

  5.   

    应该是布局参数的问题:
     pwin.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
    这边的Gravity.BOTTOM改成center试试。
      

  6.   

    http://www.girlcoding.com/2011/04/popupwindow-custom-menu/应该能满足楼主的需求