android之PopUpWindow显示Listview(文件列表)

main.xml




    

popupwindow.xml



	
    

mainActivity

package com.android.demo;

import java.io.File;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class mainActivity extends Activity {
	private Button myButton;
	private String[] name;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myButton = (Button)findViewById(R.id.button);
        myButton.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				initPopWindow();
			}});
    }
    private void initPopWindow(){
    	
    	View contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popupwindow, null);
    	contentView.setBackgroundColor(Color.BLUE);
    	
    	PopupWindow popupWindow = new PopupWindow(findViewById(R.id.mainLayout), 200, 700);
    	popupWindow.setContentView(contentView);
    	
    	TextView textView = (TextView) contentView.findViewById(R.id.text);
    	textView.setText("测试");
    	openDir();
    	ListView listView = (ListView) contentView.findViewById(R.id.list);
    	ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, name);
    	listView.setAdapter(adapter);
    	
    	popupWindow.setFocusable(true);
    	popupWindow.showAsDropDown(myButton);
    }
    
    private void openDir()
	 {
		 String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath();
		 File file  = new File(rootPath);
		 File[] files = file.listFiles();
		 name = new String[files.length];
		 for(int i=0;i

结果如图




你可能感兴趣的:(android)