如何给自定义视图类充气?

0gr*_*ity 5 android android-custom-view android-layout android-inflate

我有一个扩展的类view,它定义了一个自定义绘图(电阻).我想点击一个按钮并将其添加viewmain layout.所以我看到电阻器,如果我再次点击它会添加另一个电阻器,依此类推.但我不知道解决这个问题的最佳方法.我已经看过很多引用的问题layoutinflater,但是没有一个问题让自定义视图类膨胀(也许我正在寻找错误的东西),它总是一个xml文件. 所以我的问题是:如何ResistorViews在我的布局中添加多个,以便用户可以与它们进行交互(移动,删除,突出显示等)?

这是我尝试过的:

活动类:

public class CircuitSolverActivity extends Activity {       

@Override    
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button bAddResistor = (Button) findViewById(R.id.bAdd);        
    final LinearLayout mLayout = (LinearLayout)findViewById(R.layout.main);
    final ResistorView mResistor = new ResistorView(this, 100, 100);
    bAddResistor.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {               

            mLayout.addView(mResistor);                 

        }
    });
  }    

}
Run Code Online (Sandbox Code Playgroud)

ResistorView类:

public class ResistorView extends View{

    private Path mSymbol;
    private Paint mPaint;

    int mX, mY;

    //...Override Constructors...    
    public ResistorView(Context context, AttributeSet attrs) {
        super(context, attrs);        
        init();
    }

    public ResistorView(Context context, int x, int y){
        super(context);     
        mX = x;
        mY = y;
        init();
    }

    private void init() {

        mSymbol = new Path();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);      
        mPaint.setStrokeWidth(2);
        mPaint.setColor(-7829368);  

        mPaint.setStyle(Paint.Style.STROKE);       

        mSymbol.moveTo(0.0F, 0.0F);
        mSymbol.lineTo(0.0F, 50.0F);
        mSymbol.lineTo(16.666666F, 58.333332F);
        mSymbol.lineTo(-16.666666F, 75.0F);
        mSymbol.lineTo(16.666666F, 91.666664F);
        mSymbol.lineTo(-16.666666F, 108.33333F);
        mSymbol.lineTo(16.666666F, 124.99999F);
        mSymbol.lineTo(-16.666666F, 141.66666F);
        mSymbol.lineTo(0.0F, 150.0F);
        mSymbol.lineTo(0.0F, 200.0F);
        mSymbol.offset(mX, mY);

    }

    @Override
    protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    canvas.drawPath(mSymbol, mPaint);       
  }
}
Run Code Online (Sandbox Code Playgroud)

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"        
    android:orientation="vertical"
    android:id="@+id/main">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add RES" />


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

谢谢.

编辑:已解决*再次感谢您的帮助.*

kab*_*uko 4

膨胀(就 Android 视图而言)严格适用于 XML。如果您在代码中动态创建和添加视图对象,那么这并不是膨胀。您现在在代码中所做的事情非常接近。唯一的问题是您实例化视图一次,而听起来您每次单击时都想添加一个新视图。尝试将实例化移动到点击处理程序中:

public class CircuitSolverActivity extends Activity {       

@Override    
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Button bAddResistor = (Button) findViewById(R.id.bAdd);        
    final LinearLayout mLayout = (LinearLayout)findViewById(R.id.main);
    bAddResistor.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {               
            final ResistorView mResistor = new ResistorView(CircuitSolverActivity.this, 100, 100);
            mLayout.addView(mResistor);                 
        }
    });
  }    
}
Run Code Online (Sandbox Code Playgroud)