android以编程方式在某个位置添加自定义视图

时间:2015-11-20 08:02:51

标签: android android-custom-view

我想在我设置的某个位置放置不同的自定义视图,它将始终放在左上角。 我已尝试使用

方法
LayoutParams params = new LayoutParams(v1.getWidth(), v1.getHeight()); 
params.leftMargin = 100; 
params.topMargin = 100;
mView.addView(v1,params);

这样,左上角就消失了...... 此外,我还可以在插入的自定义视图上设置旋转吗? 请给我一些建议来处理这个问题 这是详细代码,

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draw_main);        
    myDraw = (RelativeLayout) findViewById(R.id.myDraw);
    Button btnAddRect = (Button) findViewById(R.id.btnAdd);
    btnAddRect.setOnClickListener(new OnClickListener() {       
        @Override
        public void onClick(View v) {
            LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v1 = vi.inflate(R.layout.drawbox, null);
            mfView = (SingleFingerView) v1.findViewById(R.id.draw);

            myDraw.addView(v1);
//              myDraw.addView(v1, params);
            myDraw.invalidate();
        }
    });
}

CustomView

public class SingleFingerView extends LinearLayout{
    public SingleFingerView(Context context) { this(context, null, 0); }

    public SingleFingerView(Context context, AttributeSet attrs) { this(context, attrs, 0); }

    public SingleFingerView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        this.parseAttr(context, attrs);

        View mRoot = View.inflate(context, R.layout.test_image_view, null);

        mView = (ImageView) mRoot.findViewById(R.id.view);        
        mPushView = (ImageView) mRoot.findViewById(R.id.push_view);
        mPush1DView = (ImageView) mRoot.findViewById(R.id.push1d_view);
        mFirmView = (ImageView) mRoot.findViewById(R.id.firm_view);
        mRemoveView = (ImageView) mRoot.findViewById(R.id.remove_view);

        addView(mRoot, -1, -1);
    }
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setParamsForView(widthMeasureSpec, heightMeasureSpec);
}


@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
}

主要的xml

<RelativeLayout
    android:id="@+id/myDraw"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerInside"
    android:src="@drawable/bg"
     />

1 个答案:

答案 0 :(得分:0)

试试这个,不需要让事情变得如此复杂。

这应该将您的自定义视图添加到您想要的宽度高度的relativeLayout,左边距和上边距为100px。

  public void onClick(View v) {

        mfView = new SingleFingerView(MainActivity.this);
        RelativeLayout.LayoutParams prms = new RelativeLayout.LayoutParams(width,height);
        prms.setMargins(100,100,0,0);
        myDraw.addView(mfView,prms);
    }

希望这有帮助。