2014-07-11 29 views
0

所以,球员,自定义布局在安卓按钮没有见过

我做了延伸ViewGroup.Earlier自定义布局,我做了自定义视图,但后来我发现,它不能包含孩子喜欢按钮。我确实扩展了viewgroup,因为我想添加按钮,就像在线性布局中一样,只是触摸时发光的属性。其他任何东西都会喜欢线性布局。

WrapLayout类

public class WrapLayout extends ViewGroup { 
boolean drawGlow = false; 
float glowX = 0; 
float glowY = 0; 
float radius = 20; 
Paint paint = new Paint(); 
{ 
    paint.setAntiAlias(true); 
    paint.setColor(3515877); 
    paint.setAlpha(50); 
}; 
public WrapLayout(Context context, AttributeSet attrs) { 

    super(context, attrs); 
    setWillNotDraw(false); 
} 
public WrapLayout(Context context, AttributeSet attrs, int defStyle) { 

    super(context, attrs, defStyle); 
    setWillNotDraw(false); 
} 
public WrapLayout(Context context) { 
    super(context); 
    setWillNotDraw(false); 
} 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if(drawGlow) 
     canvas.drawCircle(glowX, glowY, radius, paint); 
} 
@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    // TODO Auto-generated method stub 
} 
@Override 
public boolean onTouchEvent(MotionEvent event){ 
    if(event.getAction() == MotionEvent.ACTION_DOWN){ 
     drawGlow = true; 
    }else if(event.getAction() == MotionEvent.ACTION_UP) 
     drawGlow = false; 
    glowX = event.getX(); 
    glowY = event.getY(); 
    this.invalidate(); 
    return true; 
} 

} 

然后我初始化我activity_main.xml中文件是这样的:

<com.example.secondcustomlayout.WrapLayout 
..... 
> 
<Button 
android:id="@+id/button1" 
... 
</Button> 
</com.example.secondcustomlayout.WrapLayout> 

MainActivity

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    } 

结果:只是空白的屏幕。

解决方案:我该怎么办?

至于

回答

0

你重写onLayout但你不指定位置给孩子,这就是为什么你看不到罢了。如果你不不希望覆盖onLayout,你应该使用(extends)的具体实施ViewGroup之一,由框架

+0

我明白你的提供,你能不能回答下一个我的问题:会发光触摸这些按钮将工作或者我必须重写onTouchIntercept? – nurgasemetey