Android中的水平滚动页脚菜单

时间:2013-08-23 08:18:05

标签: android menu footer horizontal-scrolling

我想在应用程序屏幕上添加页脚菜单。页脚菜单将具有菜单图标,当按下向左或向右箭头时,菜单图标是水平可滚动的一个菜单。根据菜单屏幕,页脚菜单应滚动到该菜单屏幕的菜单图标。

例如:

    ----------------------------------------
     <   menu1     |  menu2  |   menu3   >
   -----------------------------------------

假设有6个菜单图标,一次可见3个。按下右箭头,它应该一次滚动一个项目。

如果我的屏幕与菜单4相关,则必须定位menu4。

   ----------------------------------------
     <   menu4     |  menu5  |   menu6   >
   -----------------------------------------

此外,每个菜单项都应该是可点击的。

请告诉我,如何以编程方式实现此目标。

谢谢和问候

3 个答案:

答案 0 :(得分:1)

我希望这会帮助你:

below code which has Exact Solution ( Programmatically with Explanation )用于您的答案!

我已经对您的问题进行了测试,并且正如您在问题中提到的那样完全正确!

public class MainActivity extends Activity implements OnClickListener {
    Context context;
    int scrWidth, scrWidth80by3, scrWidth10;
    Button btnMenu[], btnNext, btnPre;
    LinearLayout llRoot, llHsMain, llMenuGroup1, llMenuGroup2;
    HorizontalScrollView hScrollView;

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

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        scrWidth = dm.widthPixels;
        scrWidth = (int) (0.8 * scrWidth); // Calculating 80 % Width of Screen for Horizontal Scroll View
        scrWidth80by3 = (int) (scrWidth / 3); // Calulation 1/3 of Width of  Horizontal Scroll View for 3  menu items
        scrWidth10 = (int) (0.1 * scrWidth);// Calulation 10% Width of Screen for Previous And Next Button

        context = MainActivity.this;

        btnMenu = new Button[7];

        llMenuGroup1 = new LinearLayout(context);
        llMenuGroup2 = new LinearLayout(context);

        llMenuGroup1.setLayoutParams(new LayoutParams(scrWidth,
                LayoutParams.WRAP_CONTENT));
        llMenuGroup2.setLayoutParams(new LayoutParams(scrWidth,
                LayoutParams.WRAP_CONTENT));
        for (int i = 1; i < btnMenu.length; i++) {
            // Starting from int i=1  to Set Menu Item Counting from 1 like Menu 1 instead of Menu 0
            btnMenu[i] = new Button(context);
            btnMenu[i].setId(i);
            btnMenu[i].setOnClickListener(this);
            btnMenu[i].setLayoutParams(new LayoutParams(scrWidth80by3,
                    LayoutParams.WRAP_CONTENT));
            btnMenu[i].setText("Menu" + i);
            if (i <= 3) {
                llMenuGroup1.addView(btnMenu[i]);
            } else {
                llMenuGroup2.addView(btnMenu[i]);
            }
        }

        btnNext = new Button(context);
        btnNext.setOnClickListener(this);
        btnNext.setId(0);
        btnPre = new Button(context);
        btnPre.setOnClickListener(this);
        btnPre.setId(-1);

        //  llRoot its Main Root Layout ! 
        llRoot = new LinearLayout(context);
        llRoot.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));

        btnNext.setLayoutParams(new LayoutParams(scrWidth10,
                LayoutParams.WRAP_CONTENT));
        btnNext.setText(">");
        btnPre.setLayoutParams(new LayoutParams(scrWidth10,
                LayoutParams.WRAP_CONTENT));
        btnPre.setText("<");

        //  hScrollView its HorizontalScrollView ! 
        hScrollView = new HorizontalScrollView(context);
        hScrollView.setLayoutParams(new LayoutParams(scrWidth,
                LayoutParams.WRAP_CONTENT));

        // As HorizontalScrollView can Have one Direct Child ! so llHsMain is one Direct Child of hScrollView!
        llHsMain = new LinearLayout(context);
        llHsMain.setLayoutParams(new LayoutParams(scrWidth,
                LayoutParams.WRAP_CONTENT));

        //Adding Menu Group1 And Menu Group 2 to  llHsMain as its Direct Child of hScrollView
        llHsMain.addView(llMenuGroup1);
        llHsMain.addView(llMenuGroup2);

        //Adding Direct Child llHsMain to hScrollView
        hScrollView.addView(llHsMain);

        //  Adding views to llRoot (its Main Root Layout) ! 
        llRoot.addView(btnPre);
        llRoot.addView(hScrollView);
        llRoot.addView(btnNext);

        //Displaying llRoot (its Main Root Layout) !
        setContentView(llRoot);  
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case -1: // Previous Button Clicked
            hScrollView.scrollTo(-scrWidth, 0); // see -ve sign to scroll back horizontally only scrWidth is 80% of Total Screen
            break;
        case 0: // Next Button Clicked
            hScrollView.scrollTo(scrWidth, 0); // scrolls next horizontally only scrWidth is 80% of Total Screen
            break;

        case 1: // Menu Button1 Clicked
            Toast.makeText(getApplicationContext(), "Menu 1 Clicked",1).show();
            break;

            // repeat up to 6

        case 6:
            Toast.makeText(getApplicationContext(), "Menu 6 Clicked",1).show();
            break;
        default:
            break;
        }

    }
}

答案 1 :(得分:0)

使用HorizontalScrollView并添加LinearLayout作为子项。在LinearLayout

中添加菜单按钮

测量菜单按钮的宽度(getWidth()),单击左或右按钮使用smoothScrollBy ( width of menu button + padding ,0);滚动视图,或者也可以使用scrollBy(x,y)

答案 2 :(得分:0)

实际上我对android太新了,但我自己做了很多相关的事情。

您可以做的是,创建一个单独的布局文件,并使用 layoutInflater 将其加载到horizontal scrollview

您可以定义相应的onTouchListener菜单。

另一种方式是,您可以定义自己的菜单(与菜单文件夹下的main.xml相同)并在页脚水平滚动视图布局中对其进行充气