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);
}
結果:只是空白的屏幕。
解決方案:我該怎麼辦?
至於
我明白你的提供,你能不能回答下一個我的問題:會發光觸摸這些按鈕將工作或者我必須重寫onTouchIntercept? – nurgasemetey