如何在 Android 中设置主菜单布局? [英] How to setup a main menu layout in Android?

查看:48
本文介绍了如何在 Android 中设置主菜单布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我正在制作的应用程序,我计划有一个由 6 个不同图标组成的主菜单,每行 2 个.这与此处看到的 Twitter 主菜单布局非常相似:

For the app that I'm making, I plan on having a main menu composed of 6 different icons, with 2 per line. This is very similar to the Twitter main menu layout seen here:

所以基本上...我应该如何设置 XML?线性布局,表格布局?然后,我实际上该怎么做才能使图标和文本等距均匀?到目前为止,我已经尝试了所有我能想到的方法,但无济于事.

So basically... how should I go about setting up the XML? LinearLayout, TableLayout? And then, what do I actually do to get the icons and text to be evenly spaced and such? I've tried everything I can think of so far and to no avail.

推荐答案

是使用 GridView &TextView(使用 CompoundDrawables)——我以前这样做过:

Yes use GridView & TextView (with CompoundDrawables) -- I did this before:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <GridView android:id="@+id/grid" android:numColumns="2"
        android:horizontalSpacing="20dip" android:verticalSpacing="20dip"
        android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>

主要活动:

GridView grid = (GridView) findViewById(R.id.grid);
        grid.setAdapter(new HomeScreenShortcutAdapter());
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id) {

                startActivity(i); // Specify activity through Intent i
            }
        });

public class HomeScreenShortcutAdapter extends BaseAdapter {



        HomeScreenShortcutAdapter() {

        }

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            final Object data = getItem(position);

            if (convertView == null) {

                tv = new TextView(getApplicationContext());
                tv.setGravity(Gravity.CENTER);

            } else {
                tv = (TextView) convertView;
            }

            Drawable icon = data.icon;
            CharSequence title = data.title;

            tv.setCompoundDrawablesWithIntrinsicBounds(
                    null, icon, null, null);
            tv.setText(title);
            tv.setTag(data);

            return tv;
        }

    }

这篇关于如何在 Android 中设置主菜单布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆