无法点击我的视图

时间:2014-11-22 01:30:16

标签: android

所以我有一个带有视图参考的对象,我试图让它可点击但没有运气。如果你们能帮助我,我会很感激!

View CircleView; 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout base = new LinearLayout(this);
    base.setOrientation(LinearLayout.VERTICAL);


    CircleView = new CircleView(this);

    CircleView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(), "text",Toast.LENGTH_LONG).show();

        }
    });


    base.addView(CircleView);

}

我的班级CircleView也扩展了View。

1 个答案:

答案 0 :(得分:0)

以这种方式试试......

View CircleView; 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout base = new LinearLayout(this);
    base.setOrientation(LinearLayout.VERTICAL);

 LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
        // set LinearLayout as a root element of the screen 
        setContentView(base , linLayoutParam);
    CircleView = new CircleView(this);
 LayoutParams circleViewParam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        CircleView.setLayoutParams(circleViewParam);
    CircleView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getBaseContext(), "text",Toast.LENGTH_LONG).show();

        }
    });


    base.addView(CircleView);

}