Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之雜記(3)

Android開發之雜記(3)

編輯:關於Android編程

1,標簽

標簽閃亮登場了。當LayoutInflater遇到這個標簽時,它會跳過它,並將內的元素添加到的父元素裡。迷惑了嗎?讓我們用來替換FrameLayout,並重寫之前的XML布局:

    
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="20dip"
        android:layout_gravity="center_horizontal|bottom"
        android:padding="12dip"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="Golden Gate" />


新的代碼中,TextView和ImageView都直接添加到上一層的FrameLayout裡。雖然視覺上看起來一樣,但View的層次更加簡單了:

\

很顯然,在這個場合使用是因為Activity的ContentView的父元素始終是FrameLayout。如果你的布局使用LinearLayout作為它的根標簽(舉例),那麼你就不能使用這個技巧。在其它的一些場合也很有用的。例如,它與標簽結合起來就能表現得很完美。你還可以在創建一個自定義的組合View時使用。讓我們看一個使用創建一個新View的例子——OkCancelBar,包含兩個按鈕,並可以設置按鈕標簽。下面的XML用於在一個圖片上顯示自定義的View:


    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">
    
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"
        android:paddingTop="8dip"
        android:gravity="center_horizontal"
        android:background="#AA000000"
        okCancelBar:okLabel="Save"
        okCancelBar:cancelLabel="Don't save" />


新的布局效果如下圖所示:

\

OkCancelBar的代碼很簡單,因為這兩個按鈕在外部的XML文件中定義,通過LayoutInflate類導入。如下面的代碼片段所示,R.layout.okcancelbar以OkCancelBar為父元素:

public class OkCancelBar extends LinearLayout {
    public OkCancelBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER);
        setWeightSum(1.0f);
        
        LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
        
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.OkCancelBar, 0, 0);
        
        String text = array.getString(R.styleable.OkCancelBar_okLabel);
        if (text == null) text = "Ok";
        ((Button) findViewById(R.id.okcancelbar_ok)).setText(text);
        
        text = array.getString(R.styleable.OkCancelBar_cancelLabel);
        if (text == null) text = "Cancel";
        ((Button) findViewById(R.id.okcancelbar_cancel)).setText(text);
        
        array.recycle();
    }
}

兩個按鈕的定義如下面的XML所示。正如你所看到的,我們使用標簽直接添加兩個按鈕到OkCancelBar。每個按鈕都是從外部相同的XML布局文件包含進來的,便於維護;我們只是簡單地重寫它們的id:

    
        layout="@layout/okcancelbar_button"
        android:id="@+id/okcancelbar_ok" />        
    
        layout="@layout/okcancelbar_button"
        android:id="@+id/okcancelbar_cancel" />


我們創建了一個靈活且易於維護的自定義View,它有著高效的View層次:

\

標簽極其有用。然而它也有以下兩個限制:

· 只能作為XML布局的根標簽使用

· 當Inflate以開頭的布局文件時,必須指定一個父ViewGroup,並且必須設定attachToRoot為true(參看inflate(int, android.view.ViewGroup, Boolean)方法)。

2,多級下拉列表

核心類: 下載地址:http://download.csdn.net/detail/minimicall/7956483

package com.example.view;

import java.util.ArrayList;

import com.example.expandtabview.R;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * 菜單控件頭部,封裝了下拉動畫,動態生成頭部按鈕個數
 * 
 * @author yueyueniao
 */

public class ExpandTabView extends LinearLayout implements OnDismissListener {
	private static final String TAG = "ExpandTabView";
	private ToggleButton selectedButton;
	private ArrayList mTextArray = new ArrayList();
	private ArrayList mViewArray = new ArrayList();
	private ArrayList mToggleButton = new ArrayList();
	private Context mContext;
	private final int SMALL = 0;
	private int displayWidth;
	private int displayHeight;
	private PopupWindow popupWindow;
	private int selectPosition;

	public ExpandTabView(Context context) {
		super(context);
		init(context);
	}

	public ExpandTabView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * 根據選擇的位置設置tabitem顯示的值
	 */
	public void setTitle(String valueText, int position) {
		if (position < mToggleButton.size()) {
			mToggleButton.get(position).setText(valueText);
		}
	}

	public void setTitle(String title){
		
	}
	/**
	 * 根據選擇的位置獲取tabitem顯示的值
	 */
	public String getTitle(int position) {
		if (position < mToggleButton.size() && mToggleButton.get(position).getText() != null) {
			return mToggleButton.get(position).getText().toString();
		}
		return "";
	}

	/**
	 * 設置tabitem的個數和初始值
	 */
	public void setValue(ArrayList textArray, ArrayList viewArray) {
		if (mContext == null) {
			return;
		}
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mTextArray = textArray;
		for (int i = 0; i < viewArray.size(); i++) {
			final RelativeLayout r = new RelativeLayout(mContext);
			int maxHeight = (int) (displayHeight * 0.7);
			RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, maxHeight);
			rl.leftMargin = 10;
			rl.rightMargin = 10;
			r.addView(viewArray.get(i), rl);
			mViewArray.add(r);
			r.setTag(SMALL);
			ToggleButton tButton = (ToggleButton) inflater.inflate(R.layout.toggle_button, this, false);
			addView(tButton);
			View line = new TextView(mContext);
			line.setBackgroundResource(R.drawable.choosebar_line);
			if (i < viewArray.size() - 1) {
				LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(2, LinearLayout.LayoutParams.FILL_PARENT);
				addView(line, lp);
			}
			mToggleButton.add(tButton);
			tButton.setTag(i);
			tButton.setText(mTextArray.get(i));

			r.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onPressBack();
				}
			});

			r.setBackgroundColor(mContext.getResources().getColor(R.color.popup_main_background));
			tButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					// initPopupWindow();
					ToggleButton tButton = (ToggleButton) view;

					if (selectedButton != null && selectedButton != tButton) {
						selectedButton.setChecked(false);
					}
					selectedButton = tButton;
					selectPosition = (Integer) selectedButton.getTag();
					startAnimation();
					if (mOnButtonClickListener != null && tButton.isChecked()) {
						mOnButtonClickListener.onClick(selectPosition);
					}
				}
			});
		}
	}

	private void startAnimation() {

		if (popupWindow == null) {
			popupWindow = new PopupWindow(mViewArray.get(selectPosition), displayWidth, displayHeight);
			popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
			popupWindow.setFocusable(false);
			popupWindow.setOutsideTouchable(true);
		}

		if (selectedButton.isChecked()) {
			if (!popupWindow.isShowing()) {
				showPopup(selectPosition);
			} else {
				popupWindow.setOnDismissListener(this);
				popupWindow.dismiss();
				hideView();
			}
		} else {
			if (popupWindow.isShowing()) {
				popupWindow.dismiss();
				hideView();
			}
		}
	}

	private void showPopup(int position) {
		View tView = mViewArray.get(selectPosition).getChildAt(0);
		if (tView instanceof ViewBaseAction) {
			ViewBaseAction f = (ViewBaseAction) tView;
			f.show();
		}
		if (popupWindow.getContentView() != mViewArray.get(position)) {
			popupWindow.setContentView(mViewArray.get(position));
		}
		popupWindow.showAsDropDown(this, 0, 0);
	}

	/**
	 * 如果菜單成展開狀態,則讓菜單收回去
	 */
	public boolean onPressBack() {
		if (popupWindow != null && popupWindow.isShowing()) {
			popupWindow.dismiss();
			hideView();
			if (selectedButton != null) {
				selectedButton.setChecked(false);
			}
			return true;
		} else {
			return false;
		}

	}

	private void hideView() {
		View tView = mViewArray.get(selectPosition).getChildAt(0);
		if (tView instanceof ViewBaseAction) {
			ViewBaseAction f = (ViewBaseAction) tView;
			f.hide();
		}
	}

	private void init(Context context) {
		mContext = context;
		displayWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();
		displayHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
		setOrientation(LinearLayout.HORIZONTAL);
	}

	@Override
	public void onDismiss() {
		showPopup(selectPosition);
		popupWindow.setOnDismissListener(null);
	}

	private OnButtonClickListener mOnButtonClickListener;

	/**
	 * 設置tabitem的點擊監聽事件
	 */
	public void setOnButtonClickListener(OnButtonClickListener l) {
		mOnButtonClickListener = l;
	}

	/**
	 * 自定義tabitem點擊回調接口
	 */
	public interface OnButtonClickListener {
		public void onClick(int selectPosition);
	}

}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved