0

我定义了一个扩展 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);

但它没有显示。我敢肯定,我错过了一些非常明显的东西。

4

1 回答 1

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);
}
于 2012-04-06T18:47:56.087 回答