如何建立在Android的主菜单布局?主菜、布局、Android

2023-09-03 23:29:46 作者:渴了

有关,我正在做的应用程序,我计划有一个主菜单的6个不同的图标组成,具有2每行。这是非常相似的微主菜单布局在这里看到:

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?的LinearLayout,TableLayout?然后,我该怎么做实际拿到的图标和文字均匀分布等?我用尽了一切我能想到的,到目前为止,并没有用。

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中:

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:

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

    }