如何在Android应用程序中设置图标

时间:2012-06-08 10:34:14

标签: android icons

  

可能重复:
  A grid layout of icon/text buttons

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

2 个答案:

答案 0 :(得分:0)

你去吧

你需要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
    };
}

答案 1 :(得分:0)

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

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

结果:

screen icons