WenJieWangFlyToWorld

导航

BaseAdapter

作者通过Java代码来实现布局,而我习惯通过.xml文件来实现,所以我对程序做了如下修改

MainActivity.java

  1. public class MainActivity extends Activity
  2. {
  3. ListView myList;
  4. TextView textView;
  5. LayoutInflater inflater=null;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main1);
  11. myList = (ListView) findViewById(R.id.myList);
  12. BaseAdapter adapter = new BaseAdapter()
  13. {
  14. @Override
  15. public int getCount()
  16. {
  17. // 指定一共包含40个选项
  18. return 40;
  19. }
  20. @Override
  21. public Object getItem(int position)
  22. {
  23. return null;
  24. }
  25. // 重写该方法,该方法的返回值将作为列表项的ID
  26. @Override
  27. public long getItemId(int position)
  28. {
  29. return position;
  30. }
  31. // 重写该方法,该方法返回的View将作为列表框
  32. @Override
  33. public View getView(int position, View convertView , ViewGroup parent)
  34. {
  35. // 创建一个LinearLayout,并向其中添加两个组件 Java代码
  36. // LinearLayout line = new LinearLayout(MainActivity.this);
  37. // line.setOrientation(0);
  38. // ImageView image = new ImageView(MainActivity.this);
  39. // image.setImageResource(R.drawable.tiger);
  40. // TextView text = new TextView(MainActivity.this);
  41. // text.setText("" + (position +1 ) + "个列表项");
  42. // text.setTextSize(20);
  43. // text.setTextColor(Color.RED);
  44. // line.addView(image);
  45. // line.addView(text);
  46. inflater = LayoutInflater.from(MainActivity.this);
  47. if (convertView == null) {
  48. convertView = inflater.inflate(R.layout.linearlayout1,null);
  49. }
  50. textView = (TextView) convertView.findViewById(R.id.tex);
  51. textView.setText("" + (position +1 ) + "个列表项");
  52. // 返回LinearLayout实例
  53. return convertView;
  54. }
  55. };
  56. myList.setAdapter(adapter);
  57. }
  58. }

 

XML文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <ListView
  7. android:id="@+id/myList"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"/>
  10. </LinearLayout>

 

 

 

linearlayout1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6.  
  7. <ImageView
  8. android:id="@+id/img"
  9. android:src="@drawable/tiger"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"/>
  12.  
  13. <TextView
  14. android:id="@+id/tex"
  15.  
  16. android:textSize="20dp"
  17. android:textColor="@android:color/holo_red_light"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"/>
  20.  
  21.  
  22. </LinearLayout>

 

 

效果:

 

posted on 2017-06-01 16:14  WenjieWangFlyToWorld  阅读(175)  评论(0编辑  收藏  举报