2014-05-08 130 views
0

我创建了自定义的幻灯片菜单。我用FrameLayout来显示我的自定义幻灯片菜单。我有一个问题。在一个小菜单中,我的幻灯片菜单完美地工作,但是在大型设备中片段不起作用。我无法显示碎片组件(例如按钮等)。 这是我的代码:Android自定义幻灯片菜单

<com.android.slidingmenuexample.MainLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/geo" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/geocell" /> 

    <ListView 
     android:id="@+id/menu_listview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/header" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button_menu" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/menumm" 
      android:onClick="toggleMenu" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/activity_main_content_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </FrameLayout> 
</LinearLayout> 

public class MainLayout extends LinearLayout { 

private static final int SLIDING_DURATION = 500; 
private static final int QUERY_INTERVAL = 16; 
int mainLayoutWidth; 
private View menu; 
private View content; 
private static int menuRightMargin = 45; 

private enum MenuState { 
    HIDING, HIDDEN, SHOWING, SHOWN, 
}; 

private int contentXOffset; 
private MenuState currentMenuState = MenuState.HIDDEN; 
private Scroller menuScroller = new Scroller(this.getContext(), 
     new EaseInInterpolator()); 
private Runnable menuRunnable = new MenuRunnable(); 
private Handler menuHandler = new Handler(); 
int prevX = 0; 
boolean isDragging = false; 
int lastDiffX = 0; 

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

public MainLayout(Context context) { 
    super(context); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec); 
    menuRightMargin = mainLayoutWidth * 50/100; 
} 

@Override 
protected void onAttachedToWindow() { 
    super.onAttachedToWindow(); 

    menu = this.getChildAt(0); 
    content = this.getChildAt(1); 
    content.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return MainLayout.this.onContentTouch(v, event); 
     } 
    }); 
    menu.setVisibility(View.GONE); 
} 

@Override 
protected void onLayout(boolean changed, int left, int top, int right, 
     int bottom) { 
    if (changed) { 
     LayoutParams contentLayoutParams = (LayoutParams) content 
       .getLayoutParams(); 
     contentLayoutParams.height = this.getHeight(); 
     contentLayoutParams.width = this.getWidth(); 
     LayoutParams menuLayoutParams = (LayoutParams) menu 
       .getLayoutParams(); 
     menuLayoutParams.height = this.getHeight(); 
     menuLayoutParams.width = this.getWidth() - menuRightMargin; 
    } 
    menu.layout(left, top, right - menuRightMargin, bottom); 
    content.layout(left + contentXOffset, top, right + contentXOffset, 
      bottom); 

} 

public void toggleMenu() { 

    if (currentMenuState == MenuState.HIDING 
      || currentMenuState == MenuState.SHOWING) 
     return; 

    switch (currentMenuState) { 
    case HIDDEN: 
     currentMenuState = MenuState.SHOWING; 
     menu.setVisibility(View.VISIBLE); 
     menuScroller.startScroll(0, 0, menu.getLayoutParams().width, 0, 
       SLIDING_DURATION); 
     break; 
    case SHOWN: 
     currentMenuState = MenuState.HIDING; 
     menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, 
       SLIDING_DURATION); 
     break; 
    default: 
     break; 
    } 
    menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL); 
    this.invalidate(); 
} 

protected class MenuRunnable implements Runnable { 
    @Override 
    public void run() { 
     boolean isScrolling = menuScroller.computeScrollOffset(); 
     adjustContentPosition(isScrolling); 
    } 
} 

private void adjustContentPosition(boolean isScrolling) { 
    int scrollerXOffset = menuScroller.getCurrX(); 

    content.offsetLeftAndRight(scrollerXOffset - contentXOffset); 

    contentXOffset = scrollerXOffset; 
    this.invalidate(); 
    if (isScrolling) 
     menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL); 
    else 
     this.onMenuSlidingComplete(); 
} 

private void onMenuSlidingComplete() { 
    switch (currentMenuState) { 
    case SHOWING: 
     currentMenuState = MenuState.SHOWN; 
     break; 
    case HIDING: 
     currentMenuState = MenuState.HIDDEN; 
     menu.setVisibility(View.GONE); 
     break; 
    default: 
     return; 
    } 
} 

protected class EaseInInterpolator implements Interpolator { 
    @Override 
    public float getInterpolation(float t) { 
     return (float) Math.pow(t - 1, 5) + 1; 
    } 

} 

public boolean isMenuShown() { 
    return currentMenuState == MenuState.SHOWN; 
} 

public boolean onContentTouch(View v, MotionEvent event) { 
    if (currentMenuState == MenuState.HIDING 
      || currentMenuState == MenuState.SHOWING) 
     return false; 
    int curX = (int) event.getRawX(); 
    int diffX = 0; 

    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 

     prevX = curX; 
     return true; 

    case MotionEvent.ACTION_MOVE: 
     if (!isDragging) { 
      isDragging = true; 
      menu.setVisibility(View.VISIBLE); 
     } 
     diffX = curX - prevX; 
     if (contentXOffset + diffX <= 0) { 
      diffX = -contentXOffset; 
     } else if (contentXOffset + diffX > mainLayoutWidth 
       - menuRightMargin) { 
      diffX = mainLayoutWidth - menuRightMargin - contentXOffset; 
     } 
     content.offsetLeftAndRight(diffX); 
     contentXOffset += diffX; 
     this.invalidate(); 

     prevX = curX; 
     lastDiffX = diffX; 
     return true; 

    case MotionEvent.ACTION_UP: 
     Log.d("MainLayout.java onContentTouch()", "Up lastDiffX " 
       + lastDiffX); 

     if (lastDiffX > 0) { 
      currentMenuState = MenuState.SHOWING; 
      menuScroller.startScroll(contentXOffset, 0, 
        menu.getLayoutParams().width - contentXOffset, 0, 
        SLIDING_DURATION); 
     } else if (lastDiffX < 0) { 
      currentMenuState = MenuState.HIDING; 
      menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0, 
        SLIDING_DURATION); 
     } 
     menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL); 
     this.invalidate(); 
     isDragging = false; 
     prevX = 0; 
     lastDiffX = 0; 
     return true; 

    default: 
     break; 
    } 

    return false; 
} 

}

而且MainActivity Java类

ublic class MainActivity extends FragmentActivity { 

MainLayout mLayout; 

ProgressDialog dialog; 
Button btMenu; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    mLayout = (MainLayout) this.getLayoutInflater().inflate(
      R.layout.activity_main, null); 
    setContentView(mLayout); 
    dialog = ProgressDialog.show(this, "Please Wait... ", "Loading... "); 
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 


    btMenu = (Button) findViewById(R.id.button_menu); 
    btMenu.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // Show/hide the menu 
      toggleMenu(v); 
     } 
    }); 

    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     public void run() { 

      FragmentManager fm = MainActivity.this 
        .getSupportFragmentManager(); 
      FragmentTransaction ft = fm.beginTransaction(); 
      Layout1 fragment = new Layout1(); 
      ft.add(R.id.activity_main_content_fragment, fragment); 
      ft.commit(); 

      if (dialog != null) { 
       dialog.dismiss(); 

      } 
     } 

    }, 10000); 

} 

public void toggleMenu(View v) { 
    mLayout.toggleMenu(); 
} 



@Override 
public void onBackPressed() { 
    if (mLayout.isMenuShown()) { 
     mLayout.toggleMenu(); 
    } else { 
     super.onBackPressed(); 
    } 
} 

} 我有问题只大屏幕分辨率。在一个完美的中小屏幕分辨率程序中工作。如果我在大屏幕上触摸layot然后工作完美,但如果我单击按钮tu调用toggleMenu功能,然后不工作。在我的选项问题是toggleMenu功能,但我不知道什么是错的

回答

0

您需要维护不同的文件夹以支持Android中的多个屏幕。

以下是应用程序中的资源目录列表,它为不同的屏幕尺寸提供不同的版面设计,并为中等,高和超高密度屏幕提供不同的位图绘图。

res/layout/my_layout.xml    // layout for normal screen size ("default") 
res/layout-small/my_layout.xml  // layout for small screen size 
res/layout-large/my_layout.xml  // layout for large screen size 
res/layout-xlarge/my_layout.xml  // layout for extra large screen size 
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation 

res/drawable-mdpi/my_icon.png  // bitmap for medium density 
res/drawable-hdpi/my_icon.png  // bitmap for high density 
res/drawable-xhdpi/my_icon.png  // bitmap for extra high density 

欲了解更多信息请参阅本link

+0

我创造了这个文件夹,并把我的lauput XML文件,但不能working.meybe问题是源,因为如果在布局触摸然后工作完美,但如果我点击按钮调用toggleMenu()函数,然后不工作 – user3607335