What I would similar to attain correct straight off is a card using grid view, as well as here's how I code it.
1.) We involve a MainActivity where nosotros volition charge the GridView:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView menuGrid = (GridView) findViewById(R.id.menu); menuGrid.setAdapter(new MenuAdapter(this)); menuGrid.setOnItemClickListener(new OnItemClickListener() { populace void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show(); } }); }
2.) The MenuAdapter, that volition serve equally the interface betwixt the information (string array, db) as well as the view:
public cast MenuAdapter extends BaseAdapter { someone Context context; someone String[] labels; someone int[] images = { R.drawable.menu_1, R.drawable.menu_2, R.drawable.menu_3, R.drawable.menu_4 }; populace MenuAdapter(Context context) { this.context = context; this.labels = context.getResources().getStringArray(R.array.menu_items); } @Override populace int getCount() { furnish this.labels.length; } @Override populace Object getItem(int arg0) { furnish null; } @Override populace long getItemId(int position) { furnish 0; } @Override populace View getView(int position, View convertView, ViewGroup parent) { TextView textView = (TextView) convertView; if (textView == null) { textView = (TextView) View.inflate(context, R.layout.menu_item, null); } textView.setText(this.labels[position]); textView.setCompoundDrawablesWithIntrinsicBounds(0, images[position], 0, 0); furnish textView; } }
3.) The layouts/views: MainActivity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/menu" /> </RelativeLayout>Menu (Note that nosotros role chemical compound stance (icon/text))
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="25dp" android:horizontalSpacing="5dp" android:numColumns="2" android:verticalSpacing="20dp" />MenuItem
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menuItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:drawableTop="@drawable/icon" android:gravity="center_horizontal" android:text="@string/common_label" android:textColor="@color/menu_item" />
4.) And the resource: strings
<string-array name="menu_items"> <item>Menu 1</item> <item>Menu 2</item> <item>Menu 3</item> <item>Menu 4</item> </string-array>
0 komentar:
Please comment if there are any that need to be asked.