在自定义视图的顶部叠加编辑视图



我尝试用EditView覆盖我的自定义视图取得了不同的成功。

该解决方案需要包含这样一个事实,即我的自定义视图执行绘制画布时执行的各种计算。计算,例如计算中心 X 和 Y 绳索、画布尺寸等。

这些计算将用于在屏幕上定位EditText

到目前为止,我已经发现RelativeLayout是覆盖方面的方法。

到目前为止,最好的解决方案(并不理想)是 创建自定义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);
    }
}

这基本上是有效的,EditText位于这些静态坐标处。但是,我希望将 EditText 放置在v_speedometer.centerXv_speedometer.centerY坐标处,但它们为 0,因为尚未绘制v_speedometer视图!!

我应该怎么做?

如果你还没有找到任何解决方案,你可以编写一个接口,它将充当你的车速表吸引了听众。这意味着每当绘制速度表时,它都会向实现EditText的类发布通知,然后在自定义视图或UI中再次设置它们的坐标。

最新更新