0

可能重复:
图标/文本按钮的网格布局

我正在制作一个有 9 个图标的 Android 应用程序,我想将它们排列成 3*3 行和列。如何做到这一点,它可以在任何 Android 手机上填满整个屏幕,而无需担心尺寸。

4

2 回答 2

1

干得好

您将需要 GridView 。

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="3"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

您的活动将如下所示。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

ImageAdapter 将如下所示。

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
}
于 2012-06-08T10:39:29.907 回答
0

或者你可以使用LinearLayouts:一个主要的垂直LinearLayout包含三个水平LinearLayouts,每个包含三个图标。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:background="#FFFFFF" 
 android:orientation="vertical" 
 android:layout_height="match_parent" 
 android:layout_width="match_parent">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
<Button 
       android:id="@+id/icon_1"
       android:background="@drawable/icon_1" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 1" 
       ></Button>
<Button 
       android:id="@+id/icon_2"
       android:background="@drawable/icon_2" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 2" 
       ></Button>
<Button 
       android:id="@+id/icon_3"
       android:background="@drawable/icon_3" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 3" 
       ></Button>
</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
<Button 
       android:id="@+id/icon_4"
       android:background="@drawable/icon_4" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 4" 
       ></Button>
 <Button 
       android:id="@+id/icon_5"
       android:background="@drawable/icon_5" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1"
       android:text="Icon 5" 
       ></Button>
 <Button 
       android:id="@+id/icon_6"
       android:background="@drawable/icon_6" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 6" 
       ></Button>
</LinearLayout>
<LinearLayout
    android:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
<Button 
       android:id="@+id/icon_7"
       android:background="@drawable/icon_7" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 7" 
       ></Button>
 <Button 
       android:id="@+id/icon_8"
       android:background="@drawable/icon_8" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 8" 
       ></Button>
 <Button 
       android:id="@+id/icon_9"
       android:background="@drawable/icon_9" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:textSize="15sp" 
       android:layout_weight="1" 
       android:text="Icon 9" 
       ></Button>
</LinearLayout>
</LinearLayout>

结果:

屏幕图标

于 2012-06-08T10:58:59.277 回答