Android開發之雜記(3),android開發雜記
1,<merge/>標籤
<merge />標籤閃亮登場了。當LayoutInflater遇到這個標籤時,它會跳過它,並將<merge />內的元素添加到<merge />的父元素裡。迷惑了嗎?讓我們用<merge />來替換FrameLayout,並重寫之前的XML布局:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/golden_gate" />
<TextView
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" />
</merge>
新的代碼中,TextView和ImageView都直接添加到上一層的FrameLayout裡。雖然視覺上看起來一樣,但View的層次更加簡單了:
很顯然,在這個場合使用<merge />是因為Activity的ContentView的父元素始終是FrameLayout。如果你的布局使用LinearLayout作為它的根標籤(舉例),那麼你就不能使用這個技巧。<merge />在其它的一些場合也很有用的。例如,它與<include />標籤結合起來就能表現得很完美。你還可以在建立一個自訂的組合View時使用<merge />。讓我們看一個使用<merge />建立一個新View的例子——OkCancelBar,包含兩個按鈕,並可以設定按鈕標籤。下面的XML用於在一個圖片上顯示自訂的View:
<merge
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="@drawable/golden_gate" />
<com.example.android.merge.OkCancelBar
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" />
</merge>
新的布局效果如所示:
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所示。正如你所看到的,我們使用<merge />標籤直接添加兩個按鈕到OkCancelBar。每個按鈕都是從外部相同的XML布局檔案包含進來的,便於維護;我們只是簡單地重寫它們的id:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<include
layout="@layout/okcancelbar_button"
android:id="@+id/okcancelbar_ok" />
<include
layout="@layout/okcancelbar_button"
android:id="@+id/okcancelbar_cancel" />
</merge>
我們建立了一個靈活且易於維護的自訂View,它有著高效的View層次:
<merge />標籤極其有用。然而它也有以下兩個限制:
· <merge />只能作為XML布局的根標籤使用
· 當Inflate以<merge />開頭的布局檔案時,必須指定一個父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<String> mTextArray = new ArrayList<String>();private ArrayList<RelativeLayout> mViewArray = new ArrayList<RelativeLayout>();private ArrayList<ToggleButton> mToggleButton = new ArrayList<ToggleButton>();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<String> textArray, ArrayList<View> 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() {@Overridepublic void onClick(View v) {onPressBack();}});r.setBackgroundColor(mContext.getResources().getColor(R.color.popup_main_background));tButton.setOnClickListener(new OnClickListener() {@Overridepublic 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);}@Overridepublic 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);}}
Android開發常識 SDK Android43(API 18)
你有聽過jdk嗎?jdk是java開發用的一個開發包,sdk是android開發用的開發包,API自我認為就是介面。(Application Programming Interface,API)是一些預先定義的函數,目的是提供應用程式與開發人員基於某軟體或硬體的以訪問一組常式的能力,而又無需訪問源碼,或理解內部工作機制的細節.。不知道是否解釋清楚。
android 30開發與android手機開發有什不同,具體需要更改什代碼?
其實吧 我覺得這樣的問題不應該放在百度知道提問,畢竟百度知道只是一個常識性的問答平台,具體問題,特別是專業性問題應該在專業論壇提問,比如你可以在csdn之類的開發人員論壇。
據我瞭解android3.0更適合平板電腦
所以現在的2.2平板有局限