如何在Android中设置主菜单布局?

时间:2010-08-21 02:03:47

标签: java android xml twitter

对于我正在制作的应用,我计划使用由6个不同图标组成的主菜单,每行2个。这与此处显示的Twitter主菜单布局非常相似: nothx

基本上......我应该如何设置XML? LinearLayout,TableLayout?然后,我实际上做了什么才能让图标和文字均匀分布?到目前为止,我已经尝试了所有我能想到的东西,但没有用。

1 个答案:

答案 0 :(得分:7)

是的,使用GridView& TextView(使用CompoundDrawables) - 之前我这样做过:

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>

MainActivity:

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

    }