編輯:中級開發
一、通過Fragment實現簡單的上下文菜單
public class FragmentContextMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContextMenuFragment content = new ContextMenuFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, content).commit(); //在Activity中通過這個與Fragment通訊
}
下面ContextMenuFragment是我們從Fragment派生的子類,裡面重寫了onCreateVIEw來布局自己的Fragment
public static class ContextMenuFragment extends Fragment {
@Override
public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,
Bundle savedInstanceState) {
VIEw root = inflater.inflate(R.layout.fragment_context_menu, container, false);
registerForContextMenu(root.findVIEwById(R.id.long_press));
return root;
}
@Override
public void onCreateContextMenu(ContextMenu menu, VIEw v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "菜單1");
menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "菜單2");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.a_item:
Log.i("CWJ", "Item 1a was chosen");
return true;
case R.id.b_item:
Log.i("CWJ", "Item 1b was chosen");
return true;
}
return super.onContextItemSelected(item);
}
}
}
涉及到的布局文件fragment_context_menu.XML源碼
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:orIEntation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextVIEw
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/fragment_context_menu_msg" />
<Button android:id="@+id/long_press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/long_press">
<requestFocus />
</Button>
</LinearLayout>
二、控制Fragment的顯示或隱藏同時包含淡入淡出效果
public class FragmentHideShow extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.fragment_hide_show);
FragmentManager fm = getFragmentManager();
addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));
//上面的兩行代碼是在Activity中為Fragment中的按鈕添加監聽事件
}
void addShowHideListener(int buttonId, final Fragment fragment) {
final Button button = (Button)findVIEwById(buttonId);
button.setOnClickListener(new OnClickListener() {
public void onClick(VIEw v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out); //為Fragment設置淡入淡出效果,Android開發網提示這裡這兩個動畫資源是android內部資源無需我們手動定義。
if (fragment.isHidden()) {
ft.show(fragment);
button.setText("隱藏");
} else {
ft.hide(fragment);
button.setText("顯示");
}
ft.commit();
}
});
}
public static class FirstFragment extends Fragment {
TextView mTextVIEw;
@Override
public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,
Bundle savedInstanceState) {
VIEw v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findVIEwById(R.id.msg);
((TextVIEw)tv).setText("The fragment saves and restores this text.");
mTextView = (TextView)v.findVIEwById(R.id.saved);
if (savedInstanceState != null) {
mTextVIEw.setText(savedInstanceState.getCharSequence("text"));
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putCharSequence("text", mTextVIEw.getText());
}
}
public static class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, VIEwGroup container,
Bundle savedInstanceState) {
VIEw v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findVIEwById(R.id.msg);
((TextView)tv).setText("The TextVIEw saves and restores this text.");
((TextView)v.findVIEwById(R.id.saved)).setSaveEnabled(true);
return v;
}
}
}
涉及資源布局文件fragment_hide_show.XML代碼:
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:orIEntation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextVIEw android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Demonstration of hiding and showing fragments." />
<LinearLayout android:orIEntation="horizontal" android:padding="4dip"
android:gravity="center_vertical" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/frag1hide"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hide" />
<fragment android:name="com.android123.FragmentHideShow$FirstFragment"
android:id="@+id/fragment1" android:layout_weight="1"
android:layout_width="0px" android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:orIEntation="horizontal" android:padding="4dip"
android:gravity="center_vertical" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Button android:id="@+id/frag2hide"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hide" />
<fragment android:name="com.android123.FragmentHideShow$SecondFragment"
android:id="@+id/fragment2" android:layout_weight="1"
android:layout_width="0px" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
有關布局文件labeled_text_edit.XML的代碼為
<?XML version="1.0" encoding="utf-8"?>
<LinearLayout XMLns:android="http://schemas.android.com/apk/res/android"
android:orIEntation="vertical" android:padding="4dip"
android:layout_width="match_parent" android:layout_height="wrap_content">
<TextVIEw android:id="@+id/msg"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip" />
<EditText android:id="@+id/saved"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/green"
android:text="@string/initial_text"
android:freezesText="true">
<requestFocus />
</EditText>
</LinearLayout>
可翻頁的產品細節屏幕為增強產品細節屏幕的可用性,我們定義了一個自定義視圖控制器(ProductScrollVIEwController 類)來支持用戶通過翻頁
在android開發應用中,默認的Button是由系統渲染和管理大小的。而我們看到的成功的移動應用,都是有著酷炫的外觀和使用體驗的。因此,我們在開發產品的時候,需要對默
對於Android 3.x honeycomb系統來說屏幕的兼容性很重要,這裡目前我們就主流的Android 1.5~2.3.4的軟件如何兼容android 3.0有關
簡介: 在這個由五個部分所組成的系列的第一部分中,您將接觸到移動 Web 應用程序中最流行的新技術:地理定位。高端智能手機都內置 GPS,現在您將了解 Web