这是我的代码,但是不知道为什么它返回null?然而,我可以在这里放一个空检查,但是有什么问题吗?
TextView descriptiontv = (TextView) findViewById(R.id.descriptiontv);
TextView tc = new TextView(c);
final PopupWindow windowPopUp = new PopupWindow(tc,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT ,false);
tc.setBackgroundResource(R.drawable.bg_training_baloon_hc_2x);
tc.setText("this is a demo test to check how it looks, i m just wanting to test whether it works or not");
tc.setPadding(20, 20, 20, 20);
LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(30, 0, 30, 0);
tc.setLayoutParams(params);发布于 2015-06-06 17:49:59
根据我的经验,在为视图设置布局参数之前,必须先将视图添加到其父视图中,如下所示:
TextView calendarYear = new TextView(getActivity());
calendarYear.setText(calendar.getYear() + "");
calendarYear.setTextSize(20);
calendarYear.setTypeface(null, Typeface.BOLD);
calendarYear.setGravity(Gravity.CENTER);
calendarYear.setTextColor(resources.getColor(android.R.color.black));
calendarItemsLinearLayout.addView(calendarYear);
// We have to add the TextView to the layout first before we
// can set the layout margins for it
ViewGroup.LayoutParams layoutParams = calendarYear.getLayoutParams();
((LinearLayout.LayoutParams)layoutParams).setMargins(0, (int)(16 * density), 0, 0);
calendarYear.setLayoutParams(layoutParams);发布于 2013-08-01 00:06:57
这里
tc.getLayoutParams()您还没有给它任何params,所以它将返回null。也许你的意思是
descriptiontv.getLayoutParams()编辑
试着改变
params.leftMargin = 30;
params.rightMargin = 30;至
params.setMargins(30, 0, 30, 0);和改变
LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams)tc.getLayoutParams();至
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);https://stackoverflow.com/questions/17975392
复制相似问题