我尝试使用DrawerLayout。我从这个项目的方法中学习http://git.oschina.net/oschina/android-app 我认为问题在于属性或风格,但我找不到它。 现在我将告诉我代码。
在activity_main.xml中:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/navigation_drawer"
android:name="com.xjtu.wangshuai.vstarcoffee.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
和fragment_navigation_drawer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?attr/layout_bg_normal">
<com.xjtu.wangshuai.vstarcoffee.widget.CustomerScrollView
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
<include layout="@layout/fragment_navigation_drawer_items"/>
</com.xjtu.wangshuai.vstarcoffee.widget.CustomerScrollView>
CustomerScrollView是一个客户小部件,它可以让列表(某些LinearLayout)可以滚动。
public class CustomerScrollView extends ScrollView {
private static final int size = 4;//表示拖动的距离为屏幕的高度的1/4
private View inner;
private float y;
private Rect normal = new Rect();
public CustomerScrollView(Context context) {
super(context);
}
public CustomerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() > 0) {
inner = getChildAt(0);
}
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (inner == null) {
return super.onTouchEvent(ev);
} else {
commOnTouchEvent(ev);
}
return super.onTouchEvent(ev);
}
public void commOnTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
y = ev.getY();
break;
case MotionEvent.ACTION_UP:
if (isNeedAnimation()) {
// Log.v("mlguitar", "will up and animation");
animation();
}
break;
case MotionEvent.ACTION_MOVE:
final float preY = y;
float nowY = ev.getY();
/**
* size=4 表示 拖动的距离为屏幕的高度的1/4
*/
int deltaY = (int) (preY - nowY) / size;
// 滚动
// scrollBy(0, deltaY);
y = nowY;
if (isNeedMove()) {
if (normal.isEmpty()) {
normal.set(inner.getLeft(), inner.getTop(),
inner.getRight(), inner.getBottom());
return;
}
int yy = inner.getTop() - deltaY;
// 移动布局
inner.layout(inner.getLeft(), yy, inner.getRight(),
inner.getBottom() - deltaY);
}
break;
default:
break;
}
}
public void animation() {
TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
normal.top);
ta.setDuration(200);
inner.startAnimation(ta);
inner.layout(normal.left, normal.top, normal.right, normal.bottom);
normal.setEmpty();
}
public boolean isNeedAnimation() {
return !normal.isEmpty();
}
public boolean isNeedMove() {
int offset = inner.getMeasuredHeight() - getHeight();
int scrollY = getScrollY();
if (scrollY == 0 || scrollY == offset) {
return true;
}
return false;
}
}
在fragment_navigation_drawer_items.xml中:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e2e2e2"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/menu_item_order_record"
style="@style/MenuItemLayoutStyle" >
<ImageView
style="@style/MenuItemImageViewStyle"
android:background="@drawable/drawer_menu_icon_blog_nor"
android:contentDescription="@null" />
<TextView
style="@style/MenuItemTextViewStyle"
android:gravity="center"
android:text="@string/drawer_order_record" />
</LinearLayout>
<View
style="@style/h_line" />
<LinearLayout
android:id="@+id/menu_item_member_points"
style="@style/MenuItemLayoutStyle" >
<ImageView
style="@style/MenuItemImageViewStyle"
android:background="@drawable/drawer_menu_icon_blog_nor"
android:contentDescription="@null" />
<TextView
style="@style/MenuItemTextViewStyle"
android:text="@string/drawer_member_points" />
</LinearLayout>
<View
style="@style/h_line" />
<LinearLayout
android:id="@+id/menu_item_member_card"
style="@style/MenuItemLayoutStyle" >
<ImageView
style="@style/MenuItemImageViewStyle"
android:background="@drawable/drawer_menu_icon_blog_nor"
android:contentDescription="@null" />
<TextView
style="@style/MenuItemTextViewStyle"
android:text="@string/drawer_menmber_card" />
</LinearLayout>
<View
style="@style/h_line" />
<LinearLayout
android:id="@+id/menu_item_message"
style="@style/MenuItemLayoutStyle" >
<ImageView
style="@style/MenuItemImageViewStyle"
android:background="@drawable/drawer_menu_icon_blog_nor"
android:contentDescription="@null" />
<TextView
style="@style/MenuItemTextViewStyle"
android:text="@string/drawer_message" />
</LinearLayout>
<View
style="@style/h_line" />
<LinearLayout
android:id="@+id/menu_item_vstar_set"
style="@style/MenuItemLayoutStyle" >
<ImageView
style="@style/MenuItemImageViewStyle"
android:background="@drawable/drawer_menu_icon_blog_nor"
android:contentDescription="@null" />
<TextView
style="@style/MenuItemTextViewStyle"
android:text="@string/drawer_vstar_set" />
</LinearLayout>
<View
style="@style/h_line" />
<LinearLayout
android:id="@+id/menu_item_settings"
style="@style/MenuItemLayoutStyle" >
<ImageView
style="@style/MenuItemImageViewStyle"
android:background="@drawable/drawer_menu_icon_blog_nor"
android:contentDescription="@null" />
<TextView
style="@style/MenuItemTextViewStyle"
android:text="@string/drawer_settings" />
</LinearLayout>
<View
style="@style/h_line" />
但是我得到了错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xjtu.wangshuai.vstarcoffee/com.xjtu.wangshuai.vstarcoffee.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2508)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2570)
at android.app.ActivityThread.access$800(ActivityThread.java:167)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.xjtu.wangshuai.vstarcoffee.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:94)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:920)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1206)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2159)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.xjtu.wangshuai.vstarcoffee.MainActivity.onCreate(MainActivity.java:60)
at android.app.Activity.performCreate(Activity.java:6021)
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 13
at android.content.res.TypedArray.getDrawable(TypedArray.java:747)
at android.view.View.<init>(View.java:3823)
at android.view.ViewGroup.<init>(ViewGroup.java:518)
at android.widget.LinearLayout.<init>(LinearLayout.java:215)
at android.widget.LinearLayout.<init>(LinearLayout.java:211)
at android.widget.LinearLayout.<init>(LinearLayout.java
答案 0 :(得分:0)
在activity_main.xml中更改片段属性
android:name =“com.xjtu.wangshuai.vstarcoffee.NavigationDrawerFragment”
到
类= “com.xjtu.wangshuai.vstarcoffee.NavigationDrawerFragment”
希望这会有所帮助......