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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3535958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:17:01  来源:igfitidea点击:

How to setup a main menu layout in Android?

javaandroidxmltwitter

提问by Kyle Hughes

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

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

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.

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

采纳答案by Sameer Segal

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

是的,使用 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;
        }

    }