您的位置:首页 > 移动开发 > Android开发

android-自定义控件中的一些问题

2012-02-29 22:41 302 查看
呵呵 一直想坚持写自己的blog 可惜始终比较懒惰/迟迟不肯更新

最近一直在学习android。 成果不大吧。 主要没有什么java基础 。以前在学校学的j2ee ,出来工作之后做了.net 一年没碰也忘干净了。

好了 言归正传 。今天写这个blog是记录一下android项目中一个bug.

由于有部分布局都是展示一个model的详细信息 ,出于重用的考虑 就把这部分做成了一个自定义控件 继承自LinearLayout

1 public class MealInfoDescLayout extends LinearLayout {

2 private Context mContext;

3 private LayoutInflater inflater;

4

5 private TextView tv_mealId;

6 private TextView tv_mealTitle;

7 private TextView tv_mealPrice;

8 private ImageButton img_btn_add;

9 private ImageButton img_btn_sub;

10 private EditText et_mealCount;

11 private ImageButton img_btn_buy;

12 /**

13 * 應對不同的佈局文件(詳情页和列表页的item項)

14 */

15 private ImageView img_defImg;

16 /**

17 * 默认菜品数量

18 */

19 private int mealcount=1;

20

21 public MealInfoDescLayout(Context context){

22 super(context);

23 //inflater =LayoutInflater.from(context);

24 mContext = context;

25 }

26 public MealInfoDescLayout(Context context, AttributeSet attrs) {

27 super(context, attrs);

28 //inflater =LayoutInflater.from(context);

29 mContext = context;

30 }

31 /**

32 * 推荐該构造函数 进行默认实例化

33 * @param context

34 * @param attrs

35 * @param resId

36 * @param imgId

37 */

38 public MealInfoDescLayout(Context context,AttributeSet attrs,int resId,int imgId){

39 super(context, attrs);

40 //inflater = LayoutInflater.from(context);

41 inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

42

43 mContext = context;

44 initView(resId, imgId);

45 }

46 private void initView(int resId,int imgId){

47 //加載佈局文件

48 inflater.inflate(resId, this);

49 Log.e(this.getClass().getName(), "Res:"+resId);

50 //加載固定佈局

51 tv_mealId = (TextView)findViewById(R.id.tv_mealId);

52 tv_mealPrice = (TextView)findViewById(R.id.tv_mealPrice);

53 tv_mealTitle = (TextView)findViewById(R.id.tv_mealTitle);

54 img_btn_add = (ImageButton)findViewById(R.id.img_btn_add);

55 img_btn_sub = (ImageButton)findViewById(R.id.img_btn_sub);

56 img_btn_buy = (ImageButton)findViewById(R.id.img_btn_buy);

57 et_mealCount = (EditText)findViewById(R.id.et_mealCount);

58 //加載動態佈局

59 img_defImg = (ImageView)findViewById(imgId);

60 }

61 public void setViewValue(MealModel model)

62 {

63 tv_mealId.setText(model.meal_id);

64 tv_mealPrice.setText("¥"+model.meal_price);

65 tv_mealTitle.setText(model.meal_title);

66 et_mealCount.setText(mealcount);

67

68 }

69 @Override

70 public void draw(Canvas canvas) {

71 // TODO Auto-generated method stub

72 super.draw(canvas);

73 }

74 /**

75 * 添加默认添加按钮监听器

76 * @author lip

77 *

78 */

79 class addListener implements OnClickListener{

80

81 public void onClick(View v) {

82 mealcount++;

83 et_mealCount.setText("¥"+mealcount);

84 }

85 }

86 /**

87 * 添加默认删除按钮监听器

88 * @author lip

89 *

90 */

91 class subListener implements OnClickListener{

92

93 public void onClick(View v) {

94 if(mealcount>0){

95 mealcount--;

96 }

97 et_mealCount.setText(mealcount);

98 }

99 }

101 }

好吧。 不太会用这个东西。。诶 接着说 。 其中 在主界面动态添加

LinearLayout layout = (LinearLayout)findViewById(R.id.layout_main);

descLayout = new MealInfoDescLayout(this, null, R.layout.meal_info_item, R.id.img_defImg);

descLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

layout.addView(descLayout);

这是主界面调用的代码

其中不太明白的是

inflater = LayoutInflater.from(context);

inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

这两种写法 很不理解吖。 用第一个方法实例inflater之后 这个控件在界面就不显示了。

后来又琢磨 既然别的控件可以用inflater实例 为什么自定义的就不可以呢 于是 就把MealInfoDescLayout类中的私有变量inflater删除掉 然后在调用的activity中 直接用MealInfoDescLayout.inflater()方法加载布局

MealInfoDescLayout.inflate(this, R.layout.meal_info_item, null); 但是发现这样子加载布局文件 依旧不能正常显示 很费解。。。

有理解的高手 给解释解释。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: