简体   繁体   English

在自定义视图上叠加EditView

[英]Overlay EditView on top of custom view

I have had varying success trying to overlay my custom view with an EditView . 尝试用EditView覆盖自定义视图时,我获得了不同的成功。

The solution needs to incorporate the fact that my custom view does various calculations which are performed when the canvas is drawn. 解决方案需要考虑以下事实:我的自定义视图会执行各种计算,这些计算是在绘制画布时执行的。 Calculations such as working out the center X and Y cordinates, dimensions of canvas etc. 诸如计算中心X和Y坐标,画布尺寸等的计算。

These calculation will be used to position the EditText on the screen. 这些计算将用于在屏幕上放置EditText

So far I have figured out that a RelativeLayout is the way to go in regard to the overlaying. 到目前为止,我已经发现RelativeLayout是覆盖方面的方法。

The best solution so far (which is not ideal) is to create a custom RelativeLayout , see below: 迄今为止最好的解决方案(不理想)是创建自定义RelativeLayout ,请参见下文:

public class MyCustomRL extends RelativeLayout {

    public MyCustomRL(Context context) {
        super(context);
        initView(context);
    }

    public MyCustomRL(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyCustomRL(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    protected void initView(Context context){

        LayoutInflater inflater = LayoutInflater.from(context);
        SeekBar_Speedometer v_speedometer = (SeekBar_Speedometer) inflater.inflate(R.layout.rl_custom_speedometer, this, false);

        addView(v_speedometer);

        RelativeLayout rl_comment = (RelativeLayout) inflater.inflate(R.layout.rl_edittext_comment, this, false);

        EditText edit = (EditText) rl_comment.findViewById(R.id.edittext_comment);
        edit.setText("Text altered at runtime");

        rl_comment.setX(112);
        rl_comment.setY(117);
        //Log.v("WHATEVER","X: "+v_speedometer.centerX); // = 0
        //rl_comment.setX(v_speedometer.centerX);
        //rl_comment.setY(v_speedometer.centerY);

        addView(rl_comment);
    }
}

This essentially works, the EditText is positioned at those static coordinates. 从本质EditTextEditText位于这些静态坐标处。 However I want the EditText to be placed at the v_speedometer.centerX and v_speedometer.centerY coordinates, but they are 0 because the v_speedometer view has not been drawn yet!!! 但是我希望将EditText放置在v_speedometer.centerXv_speedometer.centerY坐标处,但它们为0,因为尚未绘制v_speedometer视图!

How should I be doing this? 我应该怎么做?

If you didn't found any solution yet, You can write an interface which will act as your speedometer hasDrawn listener. 如果尚未找到任何解决方案,则可以编写一个界面,该界面将用作速度计的hasDrawn侦听器。 It means whenever your speedometer will be drawn just it will post notification to the class implementing your EditText and then set their coordinates again in your CustomView or UI . 这意味着无论何时绘制速度表,它都会向实现EditText的类发布通知,然后在CustomView或UI中再次设置其坐标。

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

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