【问题标题】:android add a custom view at a certain position programmaticallyandroid以编程方式在某个位置添加自定义视图
【发布时间】:2016-02-22 15:32:45
【问题描述】:

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

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

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

主活动

@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();
        }
    });
}

自定义视图

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"
     />

【问题讨论】:

    标签: android android-custom-view


    【解决方案1】:

    试试这个,没必要搞这么复杂。

    这应该将您的自定义视图添加到您的 relativeLayout 中,并具有您想要的宽度高度以及 100 像素的左边距和上边距。

      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);
        }
    

    希望这会有所帮助。

    【讨论】:

    • 我尝试这个,但是,当我 new SingleFingerView(this);没有响应发生...甚至没有添加自定义视图。
    • 这没有意义。
    • 是不是和自定义视图的构造函数设置有关导致不添加视图??你有什么例子可以让我看看这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多