仿iReader-按menu鍵彈出PopupWindow佈局界面

popupwindow

 

package com.lectek.android.read.ui;

import com.lectek.android.read.adapter.GridViewAdapter;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * 顯示PopupWindow
 * @author zlf
 *更新時間:2012-3-13
 */
public class ReadPopupWindowActivity extends Activity {
    
	private PopupWindow popupWindow;
	private TextView mTextView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏顯示
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        //		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //隱藏標題欄
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
    }
    
    /**
     * 按返回鍵的相關處理
     */   
    @Override
	public void onBackPressed() {
    	if(popupWindow.isShowing()){
    		popupWindow.dismiss();
    	}		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		initPopupWindow();
		return true;
	}

	/**
     * 彈出PopupWindow
     */
    public void initPopupWindow(){
    	//加載PopupWindow的佈局文件
    	LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	View view = layoutInflater.inflate(R.layout.popup, null);
    	//加載PopupWindow的媒介佈局文件
    	View parentView = layoutInflater.inflate(R.layout.main, null);
    	//實例化PopupWindow中的組件
    	TextView titleTextView = (TextView)view.findViewById(R.id.popup_title);
    	ImageView editImg = (ImageView)view.findViewById(R.id.popup_edit);
    	ImageView listenImg = (ImageView)view.findViewById(R.id.popup_listen);
    	
    	SeekBar seekBar = (SeekBar)view.findViewById(R.id.popup_progress);
    	TextView progressTextView = (TextView)view.findViewById(R.id.popup_progress_tv);
    	
    	GridView gridView = (GridView)view.findViewById(R.id.popup_grid);
    	GridViewAdapter adapter = new GridViewAdapter(this, mPictures, mTitles);
    	gridView.setAdapter(adapter);
    	
    	//聲明並實例化PopupWindow
    	popupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    	//爲PopupWindow設置彈出的位置
    	//popupWindow.showAsDropDown(parentView);
    	popupWindow.showAtLocation(parentView, Gravity.TOP, 0, 0);
  
    }
    
    /**
     * 圖片資源
     */
    private int[] mPictures = {R.drawable.v, R.drawable.p, R.drawable.v, 
    		R.drawable.p, R.drawable.v, R.drawable.p, R.drawable.v, R.drawable.p,};
    
    /**
     * 文字資源
     */
    private String[] mTitles = {"目錄、書籤", "字體大小", "亮度調節", "閱讀背景",
    		"贈送", "評論", "分享", "更多設置"};
}

package com.lectek.android.read.adapter;

import com.lectek.android.read.ui.R;

import android.content.ContentValues;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * GridView的適配器
 * @author zlf
 *更新時間2012-3-13
 */
public class GridViewAdapter extends BaseAdapter {
	
	private Context mContext; 
	private LayoutInflater mInflater;
	private int[] mPictures;
	private String[] mTitles;
	
	public GridViewAdapter(Context context, int[] pictures, String[] titles){
		this.mContext = context;
		this.mPictures = pictures;
		this.mTitles = titles;
		mInflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		
		return mPictures.length;
	}

	@Override
	public Object getItem(int position) {
		
		return null;
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewHolder = null;
		int picture = mPictures[position];
		String title = mTitles[position];
		
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.grid, null);
			viewHolder.imageView = (ImageView)convertView.findViewById(R.id.grid_img);
			viewHolder.textView = (TextView)convertView.findViewById(R.id.grid_tv);
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		viewHolder.imageView.setBackgroundResource(picture);
		viewHolder.textView.setText(title);
		return convertView;
	}
	
	private final class ViewHolder{
		public ImageView imageView;
		public TextView textView;
	}
}

popup.xml佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
	<LinearLayout 
	    android:id="@+id/popup_title_layout"
	    android:layout_width="fill_parent"
	    android:layout_height="0dip"
	    android:layout_weight="2"
	    android:orientation="horizontal"
	    android:background="#e0000000">
	    <TextView
	        android:layout_width="0dip"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"/>
	    <TextView 
	        android:id="@+id/popup_title"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_weight="4"	        
	        android:text="@string/main_title_word_tv"/>
	    <ImageView 
	        android:id="@+id/popup_edit"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:background="@drawable/add"
	        android:layout_weight="1"/>
	    <ImageView
	        android:id="@+id/popup_listen"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:background="@drawable/listen"/>
	</LinearLayout>
	<LinearLayout
	    android:id="@+id/popup_content_layout"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:layout_weight="8"
	    android:background="#00000000">
	    <TextView 
	        android:id="@+id/popup_content"
	        android:layout_width="fill_parent"
	        android:layout_height="360dip"/>
	</LinearLayout>
    <LinearLayout 
        android:id="@+id/popup_bottom"
        android:layout_width="fill_parent"      
        android:layout_height="0dip"
        android:orientation="vertical"
        android:background="#e0000000"
        android:layout_weight="6">
	    <LinearLayout
	        android:id="@+id/popup_progress_layout"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:layout_marginTop="5dip"
	        android:orientation="horizontal"
	        android:layout_weight="4">
        <SeekBar 
            android:id="@+id/popup_progress"
            android:layout_width="7dip"
            android:layout_height="18dip"
            android:layout_marginLeft="10dip"
            android:layout_weight="2"
            android:progressDrawable="@drawable/seekbar_style"           
            android:max="100"
            android:progress="30"/>
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/popup_progress_tv"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"           
            android:text="@string/popup_progress_page"/>
        </LinearLayout>
        <GridView 
            android:id="@+id/popup_grid"
            android:layout_width="fill_parent"
            android:layout_height="100dip"
            android:gravity="center"
            android:numColumns="4"
            android:layout_weight="4"></GridView>
    </LinearLayout>

</LinearLayout>

grid.xml佈局文件java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@drawable/grid_shape">
	<ImageView
	    android:id="@+id/grid_img"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center"
	    android:background="@drawable/v"/>    
	<TextView 
	    android:id="@+id/grid_tv"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="目錄、書籤"
	    android:layout_gravity="center"
	    android:textSize="12dip"/>
</LinearLayout>
相關文章
相關標籤/搜索