Android學習筆記(四)之碎片化Fragment實現仿人人客戶端的側邊欄
其實一種好的UI佈局,可以使使用者感到更加的親切與方便。最近非常流行的莫過於側邊欄了,其實我也做過很多側邊欄的應用,但是那些側邊欄的使用我都不是很滿意,現在重新整理,重新寫了一個相對來說我比較滿意的側邊欄,其中運用的就是android3.0版本之後新加的Fragment碎片化,碎片化的使用將來也是一個趨勢,所以通過我這個程式你既可以學到側邊欄,也能讓你更加熟悉碎片化的使用,一舉兩得的事。哈哈……廢話不多說了,直接上圖。圖片如下:
①、自定義一個View,把左側邊欄檢視,中間內容檢視,右側邊欄檢視放在裡面,這裡給這個View起名叫:SlidingMenu.java
程式碼如下:
package net.loonggg.view; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.RelativeLayout; public class SlidingMenu extends RelativeLayout { private SlidingView mSlidingView; private View mMenuView; private View mDetailView; public SlidingMenu(Context context) { super(context); } public SlidingMenu(Context context, AttributeSet attrs) { super(context, attrs); } public SlidingMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public void addViews(View left, View center, View right) { setLeftView(left); setRightView(right); setCenterView(center); } /** * 新增左側邊欄的view * * @param view */ @SuppressWarnings("deprecation") public void setLeftView(View view) { LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); behindParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);// 在父控制元件的左邊 addView(view, behindParams); mMenuView = view; } /** * 新增右側邊欄的view * * @param view */ @SuppressWarnings("deprecation") public void setRightView(View view) { LayoutParams behindParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); behindParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);// 在父控制元件的右邊 addView(view, behindParams); mDetailView = view; } /** * 新增中間內容的view * * @param view */ @SuppressWarnings("deprecation") public void setCenterView(View view) { LayoutParams aboveParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); mSlidingView = new SlidingView(getContext()); mSlidingView.setView(view); addView(mSlidingView, aboveParams); mSlidingView.setMenuView(mMenuView); mSlidingView.setDetailView(mDetailView); mSlidingView.invalidate(); } public void showLeftView() { mSlidingView.showLeftView(); } public void showRightView() { mSlidingView.showRightView(); } }
②、通過一箇中間的View,去控制左右側邊欄的滑進與滑出,這個也是自定義的一個View,名字叫:SlidingView.java
程式碼如下:
package net.loonggg.view; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.Scroller; public class SlidingView extends ViewGroup { private FrameLayout mContainer; private Scroller mScroller; private VelocityTracker mVelocityTracker; private int mTouchSlop; private float mLastMotionX; private float mLastMotionY; private static final int SNAP_VELOCITY = 1000; private View mMenuView; private View mDetailView; public SlidingView(Context context) { super(context); init(); } public SlidingView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SlidingView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mContainer.measure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int width = r - l; final int height = b - t; mContainer.layout(0, 0, width, height); } private void init() { mContainer = new FrameLayout(getContext()); mContainer.setBackgroundColor(0xff000000); mScroller = new Scroller(getContext()); mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); super.addView(mContainer); } public void setView(View v) { if (mContainer.getChildCount() > 0) { mContainer.removeAllViews(); } mContainer.addView(v); } @Override public void scrollTo(int x, int y) { super.scrollTo(x, y); postInvalidate(); } @Override public void computeScroll() { if (!mScroller.isFinished()) { if (mScroller.computeScrollOffset()) { int oldX = getScrollX(); int oldY = getScrollY(); int x = mScroller.getCurrX(); int y = mScroller.getCurrY(); if (oldX != x || oldY != y) { scrollTo(x, y); } // Keep on drawing until the animation has finished. invalidate(); } else { clearChildrenCache(); } } else { clearChildrenCache(); } } private boolean mIsBeingDragged; @Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); final float x = ev.getX(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; mIsBeingDragged = false; break; case MotionEvent.ACTION_MOVE: final float dx = x - mLastMotionX; final float xDiff = Math.abs(dx); final float yDiff = Math.abs(y - mLastMotionY); if (xDiff > mTouchSlop && xDiff > yDiff) { mIsBeingDragged = true; mLastMotionX = x; } break; } return mIsBeingDragged; } @Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); final float x = ev.getX(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_DOWN: if (!mScroller.isFinished()) { mScroller.abortAnimation(); } mLastMotionX = x; mLastMotionY = y; if (getScrollX() == -getMenuViewWidth() && mLastMotionX < getMenuViewWidth()) { return false; } if (getScrollX() == getDetailViewWidth() && mLastMotionX > getMenuViewWidth()) { return false; } break; case MotionEvent.ACTION_MOVE: if (mIsBeingDragged) { enableChildrenCache(); final float deltaX = mLastMotionX - x; mLastMotionX = x; float oldScrollX = getScrollX(); float scrollX = oldScrollX + deltaX; if (deltaX < 0 && oldScrollX < 0) { // left view final float leftBound = 0; final float rightBound = -getMenuViewWidth(); if (scrollX > leftBound) { scrollX = leftBound; } else if (scrollX < rightBound) { scrollX = rightBound; } // mDetailView.setVisibility(View.INVISIBLE); // mMenuView.setVisibility(View.VISIBLE); } else if (deltaX > 0 && oldScrollX > 0) { // right view final float rightBound = getDetailViewWidth(); final float leftBound = 0; if (scrollX < leftBound) { scrollX = leftBound; } else if (scrollX > rightBound) { scrollX = rightBound; } // mDetailView.setVisibility(View.VISIBLE); // mMenuView.setVisibility(View.INVISIBLE); } scrollTo((int) scrollX, getScrollY()); } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); int velocityX = (int) velocityTracker.getXVelocity(); velocityX = 0; Log.e("ad", "velocityX == " + velocityX); int oldScrollX = getScrollX(); int dx = 0; if (oldScrollX < 0) { if (oldScrollX < -getMenuViewWidth() / 2 || velocityX > SNAP_VELOCITY) { dx = -getMenuViewWidth() - oldScrollX; } else if (oldScrollX >= -getMenuViewWidth() / 2 || velocityX < -SNAP_VELOCITY) { dx = -oldScrollX; } } else { if (oldScrollX > getDetailViewWidth() / 2 || velocityX < -SNAP_VELOCITY) { dx = getDetailViewWidth() - oldScrollX; } else if (oldScrollX <= getDetailViewWidth() / 2 || velocityX > SNAP_VELOCITY) { dx = -oldScrollX; } } smoothScrollTo(dx); clearChildrenCache(); } break; } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } return true; } private int getMenuViewWidth() { if (mMenuView == null) { return 0; } return mMenuView.getWidth(); } private int getDetailViewWidth() { if (mDetailView == null) { return 0; } return mDetailView.getWidth(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); } public View getDetailView() { return mDetailView; } public void setDetailView(View mDetailView) { this.mDetailView = mDetailView; } public View getMenuView() { return mMenuView; } public void setMenuView(View mMenuView) { this.mMenuView = mMenuView; } // void toggle() { // int menuWidth = mMenuView.getWidth(); // int oldScrollX = getScrollX(); // if (oldScrollX == 0) { // smoothScrollTo(-menuWidth); // } else if (oldScrollX == -menuWidth) { // smoothScrollTo(menuWidth); // } // } /** * 左側邊欄的關閉與顯示 */ public void showLeftView() { int menuWidth = mMenuView.getWidth(); int oldScrollX = getScrollX(); if (oldScrollX == 0) { smoothScrollTo(-menuWidth); } else if (oldScrollX == -menuWidth) { smoothScrollTo(menuWidth); } } /** * 右側邊欄的關閉與顯示 */ public void showRightView() { int menuWidth = mDetailView.getWidth(); int oldScrollX = getScrollX(); if (oldScrollX == 0) { smoothScrollTo(menuWidth); } else if (oldScrollX == menuWidth) { smoothScrollTo(-menuWidth); } } void smoothScrollTo(int dx) { int duration = 500; int oldScrollX = getScrollX(); mScroller.startScroll(oldScrollX, getScrollY(), dx, getScrollY(), duration); invalidate(); } void enableChildrenCache() { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View layout = (View) getChildAt(i); layout.setDrawingCacheEnabled(true); } } void clearChildrenCache() { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View layout = (View) getChildAt(i); layout.setDrawingCacheEnabled(false); } } }
③、在MainActivity的佈局檔案中,引用咱們自定義的側邊欄View,MainActivity的佈局檔案程式碼如下:activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <net.loonggg.view.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/slidingMenu" android:layout_width="fill_parent" android:layout_height="fill_parent" />
④、MainActivity繼承碎片化FragmentActivity,其程式碼如下:
package net.loonggg.fragment;
import net.loonggg.view.SlidingMenu;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
public class MainActivity extends FragmentActivity {
private SlidingMenu mSlidingMenu;// 側邊欄的view
private LeftFragment leftFragment; // 左側邊欄的碎片化view
private RightFragment rightFragment; // 右側邊欄的碎片化view
private SampleListFragment centerFragment;// 中間內容碎片化的view
private FragmentTransaction ft; // 碎片化管理的事務
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 去標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mSlidingMenu = (SlidingMenu) findViewById(R.id.slidingMenu);
mSlidingMenu.setLeftView(getLayoutInflater().inflate(
R.layout.left_frame, null));
mSlidingMenu.setRightView(getLayoutInflater().inflate(
R.layout.right_frame, null));
mSlidingMenu.setCenterView(getLayoutInflater().inflate(
R.layout.center_frame, null));
ft = this.getSupportFragmentManager().beginTransaction();
leftFragment = new LeftFragment();
rightFragment = new RightFragment();
ft.replace(R.id.left_frame, leftFragment);
ft.replace(R.id.right_frame, rightFragment);
centerFragment = new SampleListFragment();
ft.replace(R.id.center_frame, centerFragment);
ft.commit();
}
public void llronclick(View v) {
switch (v.getId()) {
case R.id.llr_energy_management:
Intent intent = new Intent(this, DetailsActivity.class);
startActivity(intent);
break;
default:
break;
}
}
public void showLeft() {
mSlidingMenu.showLeftView();
}
public void showRight() {
mSlidingMenu.showRightView();
}
}
⑤、左中右,左側邊欄,中間內容部分,右側邊欄,分別用Fragment代替,呈現出你想要的介面,這裡我們只需要寫Fragment就可以了,中間部分我們用SampleListFragment代替其中間內容部分,SampleListFragment的程式碼如下:
package net.loonggg.fragment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SampleListFragment extends ListFragment {
private ImageView lv_left;
private ImageView iv_right;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.list, null);
lv_left = (ImageView) mView.findViewById(R.id.iv_left);
iv_right = (ImageView) mView.findViewById(R.id.iv_right);
return mView;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Map<String, Object> item1 = new HashMap<String, Object>();
item1.put("list_title", getString(R.string.title1));
item1.put("list_image", R.drawable.p1);
item1.put("list_contect", getString(R.string.test));
Map<String, Object> item2 = new HashMap<String, Object>();
item2.put("list_title", getString(R.string.title1));
item2.put("list_image", R.drawable.p2);
item2.put("list_contect", getString(R.string.test));
Map<String, Object> item3 = new HashMap<String, Object>();
item3.put("list_title", getString(R.string.title1));
item3.put("list_image", R.drawable.p3);
item3.put("list_contect", getString(R.string.test));
Map<String, Object> item4 = new HashMap<String, Object>();
item4.put("list_title", getString(R.string.title1));
item4.put("list_image", R.drawable.p4);
item4.put("list_contect", getString(R.string.test));
Map<String, Object> item5 = new HashMap<String, Object>();
item5.put("list_title", getString(R.string.title1));
item5.put("list_image", R.drawable.p5);
item5.put("list_contect", getString(R.string.test));
Map<String, Object> item6 = new HashMap<String, Object>();
item6.put("list_title", getString(R.string.title1));
item6.put("list_image", R.drawable.p6);
item6.put("list_contect", getString(R.string.test));
Map<String, Object> item7 = new HashMap<String, Object>();
item7.put("list_title", getString(R.string.title1));
item7.put("list_image", R.drawable.p7);
item7.put("list_contect", getString(R.string.test));
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
data.add(item1);
data.add(item2);
data.add(item3);
data.add(item4);
data.add(item5);
data.add(item6);
data.add(item7);
String[] from = new String[] { "list_title", "list_image",
"list_contect" };
int[] to = new int[] { R.id.list_title, R.id.list_image,
R.id.list_contect };
SimpleAdapter adapter = new SimpleAdapter(getActivity(), data,
R.layout.list_item, from, to);
setListAdapter(adapter);
lv_left.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((MainActivity) getActivity()).showLeft();
}
});
iv_right.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
((MainActivity) getActivity()).showRight();
}
});
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), DetailsActivity.class);
startActivity(intent);
}
}
其相應的佈局檔案程式碼如下:list.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:background="#fff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/banner_unit"
android:padding="7dip" >
<ImageView
android:id="@+id/iv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:clickable="true"
android:src="@drawable/booklist_menu_normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal"
android:textColor="#000"
android:layout_weight="0.8"
android:text="Fragment"
android:textSize="17dp" />
<ImageView
android:id="@+id/iv_right"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.1"
android:src="@drawable/back_normal" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
⑥、左側邊欄的View用LeftFragment替代,LeftFragment程式碼如下:
package net.loonggg.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class LeftFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, null);
LinearLayout userLayout = (LinearLayout) view
.findViewById(R.id.userLayout);
userLayout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
UserFragment user = new UserFragment();
FragmentTransaction ft = getActivity()
.getSupportFragmentManager().beginTransaction();
ft.replace(R.id.center_frame, user);
ft.commit();
((MainActivity) getActivity()).showLeft();
}
});
LinearLayout mainPage = (LinearLayout) view.findViewById(R.id.mainPage);
mainPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft = getActivity()
.getSupportFragmentManager().beginTransaction();
ft.replace(R.id.center_frame, new SampleListFragment());
ft.commit();
((MainActivity) getActivity()).showLeft();
}
});
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
其對應的佈局檔案程式碼如下:left_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mlist"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/mainPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/oper_title2"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="主頁"
android:textColor="#000"
android:textSize="17dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/booklist_menu_bg_unit2"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/userLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clickable="true"
android:orientation="vertical"
android:padding="5dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/booklist_menu_user" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="使用者"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clickable="true"
android:orientation="vertical"
android:padding="5dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/booklist_menu_synchronize" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="收藏"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clickable="true"
android:orientation="vertical"
android:padding="5dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/booklist_menu_std" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="標準"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clickable="true"
android:orientation="vertical"
android:padding="5dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/booklist_menu_help" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="幫助"
android:textColor="#000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:clickable="true"
android:orientation="vertical"
android:padding="5dip" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="@drawable/booklist_menu_about" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="關於"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
⑦、右側邊欄的View用RightFragment替代,RightFragment程式碼如下:
package net.loonggg.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, null);
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
其對應的佈局檔案程式碼如下:right_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/oper_title"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="服 務"
android:textColor="#000"
android:textSize="17dip" />
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/booklist_menu_bg_unit" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dip" >
<LinearLayout
android:id="@+id/llr_energy_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="能源管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<LinearLayout
android:id="@+id/llr_hr_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="人力管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<LinearLayout
android:id="@+id/llr_business_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="商務管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<LinearLayout
android:id="@+id/llr_financial_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="金融管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<LinearLayout
android:id="@+id/llr_purchasing_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="採購管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<LinearLayout
android:id="@+id/llr_supply_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="供應管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<LinearLayout
android:id="@+id/llr_project_management"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:onClick="llronclick" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:src="@drawable/lxqg" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.8"
android:text="專案管理"
android:textColor="#000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1"
android:gravity="center"
android:src="@drawable/arrow_to_right" />
</LinearLayout>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:src="@drawable/line_read_option" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="說明我是寬度自適應的..." />
</LinearLayout>
</ScrollView>
</LinearLayout>
⑧、在左側邊欄中有一個點選事件,點選使用者,進入使用者介面,這裡的使用者介面也是一個Fragment,點選事件的具體程式碼在⑥中已經有了,現在我把使用者介面的Fragment程式碼貼出來:UserFragment.java:
package net.loonggg.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class UserFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user, null);
ImageView left = (ImageView) view.findViewById(R.id.iv_user_left);
left.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((MainActivity) getActivity()).showLeft();
}
});
return view;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
其對應的佈局程式碼如下:user.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/banner_unit"
android:padding="7dip" >
<ImageView
android:id="@+id/iv_user_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:clickable="true"
android:src="@drawable/booklist_menu_normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.8"
android:gravity="center_horizontal"
android:text="Fragment"
android:textColor="#000"
android:textSize="17dp" />
<ImageView
android:id="@+id/iv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.1"
android:clickable="true"
android:src="@drawable/back_normal" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="使用者管理"
android:textColor="#000000"
android:textSize="24sp" />
</LinearLayout>
⑨、其他不重要的程式碼如下:
DetailsActivity.java:
package net.loonggg.fragment;
import android.app.Activity;
import android.os.Bundle;
public class DetailsActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
}
}
其對應的佈局檔案程式碼如下:details.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="@drawable/p1" />
</LinearLayout>
還有就是string.xml的程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">碎片化側邊欄</string>
<string name="action_settings">Settings</string>
<string name="title1">這種導彈的平均航速為5倍音速</string>
<string name="title2">這種導彈的平均航速為5倍音速</string>
<string name="title3">這種導彈的平均航速為5倍音速</string>
<string name="title4">這種導彈的平均航速為5倍音速</string>
<string name="title5">這種導彈的平均航速為5倍音速</string>
<string name="title6">這種導彈的平均航速為5倍音速</string>
<string name="title7">這種導彈的平均航速為5倍音速</string>
<string name="test">美國《空軍》雜誌、西班牙《世界報》等媒體報道,美軍計劃在今年內多次試射高超音速導彈,從而將其作為威懾中俄的強力武器。有外媒指出,解放軍已開發出可攔截高速導彈的防控系統,而且有潛力進一步開發攔截高超音速導彈的防空武器。
美國《空軍》雜誌報道,美軍將在2013年繼續試射高超音速巡航導彈,這種導彈的平均航速為5倍音速,最高瞬時航速達到7倍音速。對於目前全球所有的防衛系統來說,它都是無法防禦的導彈。美國《防務新聞》更是聲稱,美軍的“高超音速攻擊時代”即將來臨,目前各國軍隊尚無能力對這種高速攻擊目標進行有效攔截,任何導彈防禦系統在它面前都形同虛設。
西班牙《世界報》報道,從2013年3月到12月,美軍可能進行3次重要的實驗,如果一切順利,美軍可能於2014年嘗試裝備少量的高超音速導彈,這種導彈有望在未來5年內形成戰鬥力。
報道認為,美軍高超音速導彈所形成的威脅以及對世界格局的影響難以估量。美國可能在一些“敏感區域”部署這種導彈,例如東亞地區,而朝鮮和伊朗的核設施也可能成為它的攻擊目標。報道強調,美軍高超音速導彈的主要作用是對其他軍事大國實施威懾,主要是中國和俄羅斯。
據俄羅斯《軍工信使》週刊指出,解放軍在防範超音速導彈攻擊方面“已經追趕上來”,中國某國防工業公開發的代號為LD-2000的陸基防空系統,已經被證實具備實戰能力。這種防空系統可為高價值目標,包括指揮所、彈道導彈發射裝置等提供保護。LD-2000防空系統發射的導彈攔截高速飛行的導彈和飛機,尤其擅長對付雷達反射面積較小的隱身巡航導彈及高速巡航導彈。報道揣測,它可以準確攔截飛行速度超過兩倍音速的超音速導彈。通過改進其控制軟體,該系統還可用來攔截高速火箭彈和迫擊炮彈。
報道稱,LD-2000系統可加裝在軍用卡車底盤上,從而實現機動部署,在需要它的地方構建防範超音速導彈的密集導彈陣。這種導彈叢集的火力可與美軍著名的“密集陣”(專門防禦來襲空中目標的防空導彈叢集)媲美。
報道稱,LD-2000是一款針對出口市場的產品,解放軍肯定同步開發了自用型號,而且其效能必然優於LD-2000,應該可以防範速度更快的來襲目標,例如航速達到三四倍音速的巡航導彈。報道認為,這表明解放軍有能力開發用來對付美軍高超音速導彈的防禦系統。</string>
</resources>
到這裡就基本完成了。可能你現在沒有看懂,我把原始碼放出來,供大家參考。原始碼下載地址如下:
相關推薦
Android學習筆記(四)之碎片化Fragment實現仿人人客戶端的側邊欄
其實一種好的UI佈局,可以使使用者感到更加的親切與方便。最近非常流行的莫過於側邊欄了,其實我也做過很多側邊欄的應用,但是那些側邊欄的使用我都不是很滿意,現在重新整理,重新寫了一個相對來說我比較滿意的側邊欄,其中運用的就是android3.0版本之後新加的Fragme
ESP32學習筆記(四)之I2C總線
oid 操作系統 parameter ica basis condition 技術分享 col rto 上一篇帖子我們了解了一下ESP32運行多任務的操作以及現象,其實也就是一個實時操作系統。那麽,這篇帖子我們就結合“LM75a”溫度傳感器來學習一下ESP32的IIC總線吧
Servlet學習筆記(四)之請求轉發與重定向(RequestDispatcher與sendRedirect)
ServletContext可以實現請求轉發(ServletContext請求轉發相關內容見之前部落格:http://blog.csdn.net/megustas_jjc/article/details/53191657),request也可以。HttpServletRequest 和 ServletCont
Docker學習筆記(四)之容器檢視啟動終止刪除
檢視 docker ps 主要有些引數要說一下 1. 不加引數,表示檢視當前正在執行的容器 2. -a,檢視所有容器包括停止狀態的容器 3. -l,檢視最新建立的容器 4. -n=x,檢視最
Android學習筆記(四)--RecyclerView擴充套件下拉重新整理與左滑刪除
今天在使用QQ的時候就想到製作一個訊息列表的類似效果,可以實現下拉重新整理和左滑刪除效果,於是就抽空試了試。先上效果圖。 這是正在重新整理的時候。然後就會增添一個item(那個重新整理的圈是會轉的然後還可以變顏色我不會截動圖)。見下圖。 Recycl
《機器學習實戰》學習筆記(四)之Logistic(上)基礎理論及演算法推導、線性迴歸,梯度下降演算法
轉載請註明作者和出處:http://blog.csdn.net/john_bh/ 執行平臺: Windows Python版本: Python3.6 IDE: Sublime text3 一、概述 Logistic迴歸是統計學習中的經典
Android學習筆記(一)之仿正點鬧鐘時間齒輪滑動的效果
看到正點鬧鐘上的設定時間的滑動效果非常好看,自己就想做一個那樣的,在網上就開始搜資料了,看到網上有的齒輪效果的程式碼非常多,也非常難懂,我就決定自己研究一下,現在我就把我的研究成果分享給大家。我研究的這個效果出來了,而且程式碼也非常簡單,通俗易懂。效果圖如下: 首先是Ma
Opencv3程式設計入門學習筆記(四)之split通道分離Debug過程中0xC0000005記憶體訪問衝突問題
這是筆者學習《Opencv3程式設計入門》的第四篇部落格,這篇部落格主要是解決在Windows系統下VS 2013中Debug含有split分離通道色彩函式時報出的0xC0000005記憶體訪問衝突問題,問題表現如下面第一幅圖所示。 剛剛遇到這個問題的時候
kafka 學習筆記(四)之Python客戶端
今天看看Python客戶端。 程式碼在github上,和以前一樣,我們用github上寫好的程式碼進行測試 https://github.com/mumrah/kafka-python pip install git+git://github.com/mumrah/kafk
TensorFlow學習筆記(二)之視覺化(Tensorboard)
一、Tensorboard簡介 Tensorboard是TensorFlow自帶的一個強大的視覺化工具,也是一個web應用程式套件。通過將tensorflow程式輸出的日誌檔案的資訊視覺化使得tensorflow程式的理解、除錯和優化更加簡單高效。支援其七種視
Unity3D之Mecanim動畫系統學習筆記(四):Animation State
大致 面板 輸入 jpg any 動畫播放 速度 nsf 顯示 動畫的設置 我們先看看Animation Clip的一些設置: Loop time:動畫是否循環播放。 下面出現了3個大致一樣的選項: Root Transform Rotation:表示為播放動畫
Andrew Ng機器學習課程筆記(四)之神經網絡
sca 優化 介紹 www 之間 output 現在 利用 href Andrew Ng機器學習課程筆記(四)之神經網絡 版權聲明:本文為博主原創文章,轉載請指明轉載地址 http://www.cnblogs.com/fydeblog/p/7365730.html 前言
Redis學習筆記(四)——數據結構之List
ltr dex pan 返回 red 操作 數據結構 alt 區間 一、介紹 Redis列表(List)是簡單的字符串列表,按照插入順序排序。你可以添加一個元素到列表的頭部(left)或者尾部(right),一個列表最多可以包含232-1個元素(4294967295,每
Android學習筆記(四四) 第三方Java庫的使用
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
嵌入式核心及驅動開發之學習筆記(四) 規範化程式碼
前面筆者已實現了使用者程式控制led燈閃爍的驅動程式碼,但是由於程式碼不規範,顯得亂七八糟的,因此需要規範化。如果比較大一點的工程沒有規範的話,也不利於後期的跟新與維護。分析先前的程式不規範點有二: 定義的變數多而且散亂 程式沒有錯誤處理機制 C語言雖然是面向過程的語言,但是
Spring 學習筆記(四)IOC之註解方式
用註解向IOC容器增加javaBean配置,還有一些注入的註解 第一步:添加註解的jar包 spring-aop-4.3.3.RELEASE.jar 第二步:applicationContext.xml檔案中開啟註解 注意beans標籤內增加了context的名稱空間和約束 &l
MyBatis學習筆記(四)mybatis-config.xml都有哪些配置之環境配置與對映器
環境配置(environments),其中還可以配置事物,專案中大概也不會用到。 <environments default="development"> <environment id="development"> <t
安卓開發學習筆記(四):Android Stuidio無法實現隱式Intent是為什麼?
1 package com.example.lenovo.activitytest; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view
Android學習筆記(四十):Preference的使用
Preference直譯為偏好,博友建議翻譯為首選項。一些配置資料,一些我們上次點選選擇的內容,我們希望在下次應用調起的時候依然有效,無須使用者再一次進行配置或選擇。Android提供preference這個鍵值對的方式來處理這種情況,自動儲存這些資料,並立時生效,同時Andr
Android學習筆記(十三)——碎片(一)
碎片 碎片可看作另外一種形式的活動,可以建立碎片來包含檢視。 碎片總是嵌入在活動中,一般有兩種常見形式: 1、碎片A和碎片B分別處於不同的活動中,當選擇碎片A中的某一項時,觸發碎片B啟動; 2、碎片A和碎片B處於同一個活動中,共享同一活動,以建立