简体   繁体   English

android中的自定义布局:看不到按钮

[英]Custom Layout in android: button is not seen

So guys, 各位

I made custom layout which extends ViewGroup.Earlier, I made custom view, but later I discovered that it can't contain children like buttons. 我创建了自定义布局来扩展ViewGroup.Warer,我创建了自定义视图,但后来我发现它不能包含像按钮这样的子代。 I did extending viewgroup because I want to add buttons, like in linear layout, just with property of glowing on touch. 我之所以扩展视图组,是因为我想添加按钮(如线性布局),仅具有触摸时发光的属性。 Anything else will like in linear layout. 线性布局中还有其他需要的东西。

WrapLayout class : 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;
}

}

Then I initialized my activity_main.xml file like this: 然后像这样初始化我的activity_main.xml文件:

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

MainActivity : MainActivity

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

RESULT : Nothing but blank screen. 结果 :只是空白屏幕。

SOLUTION : What can I do? 解决方案 :我该怎么办?

With regards 带着敬意

you override onLayout but you not assign the position to the children, that's why you do not see nothing. 您覆盖了onLayout但未将位置分配给子级,这就是为什么您什么都看不到的原因。 If you don not want to override onLayout , you should use ( extends ) one of the concrete implementation of ViewGroup , provided by the framework 如果您不想覆盖onLayout ,则应使用( extends )框架提供的ViewGroup具体实现之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM