2012-10-03 274 views
1

我有一個自定義視圖,並希望在該自定義視圖上添加一個自定義視圖。這是我的自定義視圖類:如何在另一個自定義視圖中添加自定義視圖?

public class CustomCircle extends View{ 

float radius; 
Paint paint = new Paint(); 

String message = ""; 
public CustomCircle(Context context, AttributeSet attrs) { 
    super(context, attrs); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    canvas.drawCircle(getWidth()/2, getHeight()/2,radius, paint); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    boolean isClickInCircle = false; 
    float x = event.getX(); 
    float y = event.getY(); 

    double check = Math.sqrt((x-getWidth()/2)*(x-getWidth()/2) + (y-getHeight()/2)*(y-getHeight()/2)); 
    if (check<=radius) { 
     isClickInCircle= true; 
    } 
    if (isClickInCircle) { 

     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 

      Toast.makeText(getContext(),message, Toast.LENGTH_LONG).show(); 
      return true; 

     case MotionEvent.ACTION_UP: 
      Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show(); 
      return true; 

     default: 
      break; 
     } 
    } 

    return false; 
} 

和正在使用擴展LinearLayout另一個類:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    B root = new B(this); 
    root.addCircle(); 
    setContentView(root); 
} 

public class B extends LinearLayout { 

private Paint paint; 

public B(Context context) { 
    super(context); 
    paint = new Paint(); 
    paint.setColor(Color.WHITE); 
    paint.setStyle(Style.FILL); 
} 

public void addCircle() { 

    CustomCircle circleBlue = new CustomCircle(getContext(), null); 
    circleBlue.paint.setColor(Color.WHITE); 
    circleBlue.paint.setAntiAlias(true); 
    circleBlue.radius = 160; 
    circleBlue.message = "Clicked"; 
    addView(circleBlue); 

    CustomCircle circleRed = new CustomCircle(getContext(), null); 
    circleRed.paint.setColor(Color.RED); 
    circleRed.paint.setAntiAlias(true); 
    circleRed.radius = 80; 
    circleRed.message = "Clicked"; 
    addView(circleRed); 

} 

,我從主活動類使用調用B級

輸出只顯示一個圓圈,而不是另一個圓圈內的圓圈。我的代碼有什麼問題?

回答

1

而輸出只顯示我一圈而不是圈內 圈。

如果你想重疊的孩子,一個RelativeLayoutFrameLayout是要走的路你選錯佈局。此外,關於您的代碼:

public class B extends RelativeLayout { 
//... 
public void addCircle() { 

    // the constructor that uses the AttributeSet should be added if you use the 
    // custom component in the xml layout 
    CustomCircle circleBlue = new CustomCircle(getContext()); 
    // ... 
    // add it with LayoutParams 
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
    rlp.addRule(RelativeLayout.CENTER_IN_PARENT); 
    addView(circleBlue, rlp); 
    // the same for the other view 
} 

而且你的兩個圈將具有相同的尺寸,使他們完美地重疊(和你無法看到他們),你就需要給他們不同的維度,通過的LayoutParams,例如:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(200, 200); 

用於第一和一個:

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(100, 100); 

爲第二個。

0

是否有可能在佈局中有2個圓圈,但您只能看到一個 ,因爲它與第一個圓的顏色相同,並且它們彼此重疊?

變化circleRed.paint.setColor(Color.WHITE);circleRed.paint.setColor(Color.BLACK); ,看看它給

+0

是我改變circleRed.paint.setColor(Color.BLACK);它仍然展示我一個圓圈。 –

+0

比我會建議看@Luksprog回答 –

+0

所以交換顏色。你現在看到什麼?如果您看到的圓圈的顏色也交換了,那麼您有兩個圓圈完全重疊。 – Simon

相關問題