android:id='@+id/grid'

  android:layout_width='fill_parent'

  android:layout_height='fill_parent'

  android:verticalSpacing='35px'

  android:horizontalSpacing='5px'

  android:numColumns='auto_fit'

  android:columnWidth='100px'

  android:stretchMode='columnWidth'

  android:gravity='center'

 />

</LinearLayout>

For this grid, we take up the entire screen except for what our selection label requires. The number of columns is computed by Android (android:numColumns='auto_fit') based on 5-pixel horizontal spacing (android:horizontalSpacing='5px'), 100-pixel columns (android:columnWidth='100px'), with the columns absorbing any “slop” width left over (android:stretchMode='columnWidth').

The Java code to configure the GridView is:

public class GridDemo extends Activity

 implements AdapterView.OnItemSelectedListener {

 TextView selection;

 String[] items={'lorem', 'ipsum', 'dolor', 'sit', 'amet',

  'consectetuer', 'adipiscing', 'elit', 'morbi', 'vel',

  'ligula', 'vitae', 'arcu', 'aliquet', 'mollis',

  'etiam', 'vel', 'erat', 'placerat', 'ante',

  'porttitor', 'sodales', 'pellentesque', 'augue', 'purus'};

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  setContentView(R.layout.main);

  selection = (TextView)findViewById(R.id.selection);

  GridView g=(GridView)findViewById(R.id.grid);

  g.setAdapter(new FunnyLookingAdapter (this,

   android.R.layout.simple_list_item_1, items));

  g.setOnItemSelectedListener(this);

 }

 public void onItemSelected(AdapterView<?> parent, View v,

  int position, long id) {

  selection.setText(items[position]);

 }

 public void onNothingSelected(AdapterView<?> parent) {

  selection.setText('');

 }

 private class FunnyLookingAdapter extends ArrayAdapter {

  Context ctxt;

  FunnyLookingAdapter(Context ctxt, int resource,

   String[] items) {

   super(ctxt, resource, items);

   this.ctxt = ctxt;

  }

  public View getView(int position, View convertView,

   ViewGroup parent) {

   TextView label = (TextView)convertView;

   if (convertView==null) {

    convertView = new TextView(ctxt);

    label = (TextView)convertView;

   }

   label.setText(items[position]);

   return(convertView);

  }

 }

}

For the grid cells, rather than using auto-generated TextView widgets as in the previous sections, we create our own views, by subclassing ArrayAdapter and overriding getView (). In this case, we wrap the funny-looking strings in our own TextView widgets, just to be different. If getView() receives a TextView, we just reset its text; otherwise, we create a new TextView instance and populate it.

With the 35-pixel vertical spacing from the XML layout (android:verticalSpacing='35'), the grid overflows the boundaries of the emulator’s screen as shown in Figures 8-4 and 8-5.

Figure 8-4. The GridDemo sample application, as initially launched

Figure 8-5. The same application, scrolled to the bottom of the grid

Fields: Now with 35% Less Typing!

The AutoCompleteTextView is sort of a hybrid between the EditText (field) and the Spinner. With auto-completion, as the user types, the text is treated as a prefix filter, comparing the entered text as a prefix against a list of candidates. Matches are shown in a selection list that, like with Spinner, folds down from the field. The user can either type out an entry (e.g., something

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату