2014.01.07 ——— android开发实例之QuickActionBar

2014.01.07 ——— android开发实例之QuickActionBar
参考: https://github.com/cyrilmottier/GreenDroid

这个组件是类似于联系人里面点击头像的操作,用法如下:

MainActivity
package com.example.testquickactionbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.view.QuickAction;
import com.example.view.QuickActionBar;
import com.example.view.QuickActionWidget;
import com.example.view.QuickActionWidget.OnQuickActionClickListener;

public class MainActivity extends Activity implements OnClickListener {
	private Button mBtnTop;
	private Button mBtnBottom;
	
	private QuickActionWidget mBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}
	
	private void initView(){
		mBtnTop  = (Button)findViewById(R.id.btnTop);
		mBtnBottom = (Button)findViewById(R.id.btnBottom);
		mBtnTop.setOnClickListener(this);
		mBtnBottom.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btnBottom:
		case R.id.btnTop:
			showQuickActionBar(v);
			break;
		}
	}
	
	
	private void showQuickActionBar(View view) {
		mBar = new QuickActionBar(this);
		mBar.addQuickAction(new QuickAction(this,
				R.drawable.ic_action_share_pressed, R.string.open));
		mBar.addQuickAction(new QuickAction(this,
				R.drawable.ic_action_rename_pressed, R.string.rename));
		mBar.addQuickAction(new QuickAction(this,
				R.drawable.ic_action_move_pressed, R.string.move));
		mBar.addQuickAction(new QuickAction(this,
				R.drawable.ic_action_delete_pressed, R.string.delete));
		mBar.setOnQuickActionClickListener(mActionListener);
		mBar.show(view);
	}
	private OnQuickActionClickListener mActionListener = new OnQuickActionClickListener() {
		public void onQuickActionClicked(QuickActionWidget widget, int position) {
				switch (position) {
				case 0:
					 Toast.makeText(MainActivity.this, "打开", 0).show();
					break;
				case 1:
					Toast.makeText(MainActivity.this, "修改名字", 0).show();
					break;
				case 2:
					Toast.makeText(MainActivity.this, "移动", 0).show();
					break;
				case 3:
					Toast.makeText(MainActivity.this, "删除", 0).show();
					break;
				default:
					Toast.makeText(MainActivity.this, "错误", 0).show();
					break;
				}
		}
	};

}




main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".MainActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:id="@+id/btnTop" 
        android:layout_alignParentTop="true"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:id="@+id/btnBottom" 
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>



效果如下:

2014.01.07 ——— android开发实例之QuickActionBar


全部代码,见附件


TestQuickActionBar_new为修改了在屏幕右侧显示不正常的问题
感谢gundumw100 反馈的bug

你可能感兴趣的:(android开发实例)