标签:
ListView Layout示例:
MainActivity.java中定义待显示的数据countryArray,
在activity_main中定义ListView,activity_listview中定义TextView。
1 package com.example.shad_fnst.listviewdemo; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.widget.ArrayAdapter; 8 import android.widget.ListView; 9 10 11 public class MainActivity extends ActionBarActivity { 12 13 String[] countryArray = {"China", "India", "America", "Japan", "England"}; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 20 ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.activity_listview, countryArray); 21 ListView listView = (ListView)findViewById(R.id.country_list); 22 listView.setAdapter(arrayAdapter); 23 } 24 25 @Override 26 public boolean onCreateOptionsMenu(Menu menu) { 27 // Inflate the menu; this adds items to the action bar if it is present. 28 getMenuInflater().inflate(R.menu.menu_main, menu); 29 return true; 30 } 31 32 @Override 33 public boolean onOptionsItemSelected(MenuItem item) { 34 // Handle action bar item clicks here. The action bar will 35 // automatically handle clicks on the Home/Up button, so long 36 // as you specify a parent activity in AndroidManifest.xml. 37 int id = item.getItemId(); 38 39 //noinspection SimplifiableIfStatement 40 if (id == R.id.action_settings) { 41 return true; 42 } 43 44 return super.onOptionsItemSelected(item); 45 } 46 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 tools:context=".MainActivity" 8 android:orientation="vertical"> 9 10 <ListView 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:id="@+id/country_list"> 14 </ListView> 15 16 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- Single List Item Design--> 3 4 <TextView 5 android:id="@+id/label" 6 xmlns:android="http://schemas.android.com/apk/res/android" 7 android:layout_height="match_parent" 8 android:layout_width="match_parent" 9 android:padding="10dip" 10 android:textSize="16dip" 11 android:textStyle="bold"> 12 </TextView>
标签:
原文地址:http://www.cnblogs.com/hello-sandy/p/4700009.html