我已经定义了一个扩展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);
但它没有表现出来。我确实错过了一些明显的东西,我很确定。
答案 0 :(得分:2)
我认为问题在于您从未致电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);
}