1

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

前任。:

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

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

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

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

而且每个菜单项都应该是可点击的。

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

谢谢并恭祝安康

4

3 回答 3

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;
        }

    }
}
于 2013-08-23T11:34:42.973 回答
0

使用HorizontalScrollView并添加 LinearLayout 作为子级。在. _ _LinearLayout

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

于 2013-08-23T08:27:17.263 回答
0

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

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

您可以定义它们对应onTouchListener的菜单。

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

于 2013-08-23T08:46:11.860 回答