2012-12-28 118 views
0

我在网上找到了菜单滑块的工作代码。 但是,在这段代码中,布局是在代码中生成的,有人可以帮我将它配置在*中的布局文件中。 XML? 对不起,我的英语不好。 代码:`我需要帮助来实现xml

private boolean Menu_Displayed=false; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Display display = getWindowManager().getDefaultDisplay(); 
    final int width = display.getWidth(); 

    // menu: 
    LinearLayout li_menu = new LinearLayout(this); 
    li_menu.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    li_menu.setOrientation(1);//1 is vertical 
    li_menu.setBackgroundColor(Color.GREEN); 

    Button btn1 = new Button(this); 
    btn1.setText("button 1"); 
    btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    li_menu.addView(btn1); 

    //body: 
    final HorizontalScrollView hsv = new HorizontalScrollView(this){ 
     @Override 
     // do not let hsv consume the click itself. Then the view under the hsv will also consume the click 
     //so that the menu will be clicked 
     //when menu is not showed up, let hsv be the only view to consume the click. 
     //so that the menu will not be clicked 
     public boolean onTouchEvent(MotionEvent ev) { 
      if(Menu_Displayed){ 
       return false; 
      } 
      else{ 
       return true; 
      } 
     } 
    }; 
    hsv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    hsv.setBackgroundColor(Color.TRANSPARENT); 
    hsv.setHorizontalFadingEdgeEnabled(false); 
    hsv.setVerticalFadingEdgeEnabled(false); 

    final LinearLayout li_body = new LinearLayout(this); 
    li_body.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT)); 
    li_body.setOrientation(0);//0 is horizantal 
    li_body.setBackgroundColor(Color.TRANSPARENT); 

    hsv.addView(li_body); 

    //body: place holder transparent 
    TextView placeholder = new TextView(this); 
    placeholder.setTextColor(Color.TRANSPARENT); 
    placeholder.setLayoutParams(new LayoutParams(width-100, LayoutParams.FILL_PARENT)); 
    placeholder.setVisibility(View.INVISIBLE); 
    li_body.addView(placeholder); 

    //body: real content 
    LinearLayout li_content = new LinearLayout(this); 
    li_content.setLayoutParams(new LayoutParams(width, LayoutParams.FILL_PARENT)); 
    li_content.setOrientation(1);//1 is vertical 
    li_content.setBackgroundColor(Color.CYAN); 

    TextView tv1 = new TextView(this); 
    tv1.setText("txt 1"); 
    tv1.setTextSize(40); 
    tv1.setTextColor(Color.BLACK); 

    TextView tv2 = new TextView(this); 
    tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
    tv2.setTextSize(50); 
    tv2.setText("txt 2"); 
    tv2.setTextColor(Color.WHITE); 

    //use this button to scroll 
    Button btn_showMenu = new Button(this); 
    btn_showMenu.setText("Menu"); 
    btn_showMenu.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    btn_showMenu.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      hsv.post(new Runnable() { 

       @Override 
       public void run() { 
        if(Menu_Displayed){ 
         hsv.smoothScrollTo(width-100, 0); 
        } 
        else{ 
         hsv.smoothScrollTo(0, 0); 
        } 
        Menu_Displayed = !Menu_Displayed; 
       } 
      }); 
     } 
    }); 

    li_content.addView(tv1); 
    li_content.addView(tv2); 
    li_content.addView(btn_showMenu); 

    li_body.addView(li_content); 

    //add menu and body in to frame 
    FrameLayout mainFrame = new FrameLayout(this); 
    mainFrame.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    mainFrame.addView(li_menu); 
    mainFrame.addView(hsv); 

    //scroll to the body real content to block the menu 
    hsv.post(new Runnable() { 

     @Override 
     public void run() { 
      hsv.scrollBy(width-100, 0);    
     } 
    }); 

    setContentView(mainFrame);   
} 

`

+1

StackOverflow是在这里帮助,而不是给你完整的工作XML文件。你不明白哪种功能? – Tobrun

回答

0

这不是一个很好的实施SlidingMenu的,你可以试试这个开放的源项目:SideMenu 要实现你比如菜单交换,在计算屏幕宽度-100,宽度不能转换为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="#ff669900" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1" /> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@android:color/transparent" 
     android:fadingEdge="none" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:background="@android:color/transparent" 
      android:orientation="horizontal" > 

      <TextView 
       android:id="@+id/placeholder" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:paddingRight="100dp" /> 
     </LinearLayout> 
    </HorizontalScrollView> 

</LinearLayout> 
+0

但如何更改java代码? – user1934801

+0

您无法将Java代码完全更改为XML视图,因为您的视图正在屏幕上进行中继,并在RunTime上进行了详细测试。 –