自定义视图不显示?
问题描述:
我定义了一个扩展LinearLayout的视图,我想将它放在ViewAnimator中。麻烦的是,它没有出现。 我不使用XML的布局,所以我有一个扩展的LinearLayout,例如一类:自定义视图不显示?
public class DetailView extends LinearLayout {
ImageView mImageView;
TextView mTxtName;
public DetailView(Context context) {
super(context);
mTxtName = new TextView(context);
LinearLayout.LayoutParams lpn = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lpn.setMargins(3,3,3,3);
mTxtName.setLayoutParams(lpn);
mTxtName.setTextAppearance(context, android.R.attr.textAppearanceMedium);
mImageView = new ImageView(context);
LinearLayout.LayoutParams lpi = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lpi.setMargins(10,10,10,10);
mImageView.setLayoutParams(lpi);
mImageView.setScaleType(ScaleType.CENTER_INSIDE);
mImageView.setImageResource(R.drawable.wait);
}
然后在我的活动我补充这样说:
va = new ViewAnimator(this);
detail = new DetailView(this);
detail.setOrientation(1);
LinearLayout.LayoutParams dLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
va.setLayoutParams(dLayout);
va.addView(detail,0);
但它不” t显示。我很确定,我错过了一些显而易见的东西。
答
我认为问题在于您从未拨打addView
将子女Views
添加到您的ViewGroup
中。它会是这样的:
public DetailView(Context context) {
super(context);
mTxtName = new TextView(context);
LinearLayout.LayoutParams lpn = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lpn.setMargins(3,3,3,3);
mTxtName.setLayoutParams(lpn);
mTxtName.setTextAppearance(context, android.R.attr.textAppearanceMedium);
this.addView(mTxtName);//add the view to your viewgroup
mImageView = new ImageView(context);
LinearLayout.LayoutParams lpi = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lpi.setMargins(10,10,10,10);
mImageView.setLayoutParams(lpi);
mImageView.setScaleType(ScaleType.CENTER_INSIDE);
mImageView.setImageResource(R.drawable.wait);
this.addView(mImageView);
}
+0
就是这样 - 我知道这会很简单!谢谢 – James 2012-04-06 20:04:10
'va'是否被添加到您的布局? – wsanville 2012-04-06 18:35:45