簡體   English   中英

在自定義視圖上疊加EditView

[英]Overlay EditView on top of custom view

嘗試用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);
    }
}

從本質EditTextEditText位於這些靜態坐標處。 但是我希望將EditText放置在v_speedometer.centerXv_speedometer.centerY坐標處,但它們為0,因為尚未繪制v_speedometer視圖!

我應該怎么做?

如果尚未找到任何解決方案,則可以編寫一個界面,該界面將用作速度計的hasDrawn偵聽器。 這意味着無論何時繪制速度表,它都會向實現EditText的類發布通知,然后在CustomView或UI中再次設置其坐標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM