动态布局单击“侦听器”不起作用

时间:2016-06-20 13:50:07

标签: android

我已经创建了一个动态布局,当我在来电时,我添加了一个ImageView,但它的点击监听器正在工作。

 private WindowManager.LayoutParams getWindowLayout() {
    if (prms != null) {
        return prms;
    }
    prms = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
           // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
    Display display = wm.getDefaultDisplay();
    int width = display.getWidth();

    prms.height = 420;
    prms.width = width;
    prms.format = PixelFormat.TRANSLUCENT;
    prms.gravity = Gravity.CENTER;
    return prms;
}

private LinearLayout getManualLinearLayout() {
    if (ly1 != null) {
        return ly1;
    }
    GradientDrawable shape = new GradientDrawable();
    shape.setCornerRadius(10);
    shape.setColor(backgroundcolor);
    shape.setStroke(1, c.getResources().getColor(R.color.red));

    ly1 = new LinearLayout(c);
    ly1.setOrientation(LinearLayout.VERTICAL);
    ly1.setPadding((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, c.getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, c.getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, c.getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15, c.getResources().getDisplayMetrics()));
    ly1.setClickable(true);
    LinearLayout wrapper = new LinearLayout(c);
    wrapper.setBackgroundColor(c.getResources().getColor(R.color.red));
    wrapper.setOrientation(LinearLayout.VERTICAL);
    wrapper.setBackground(shape);

    LinearLayout head = new LinearLayout(c);
    head.setOrientation(LinearLayout.HORIZONTAL);
    head.setPadding(1, 0, 7, 0);
    TextView title = new TextView(c);
    title.setText("UC");
    title.setTypeface(face2);
    LinearLayout.LayoutParams t1LayoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
    title.setLayoutParams(t1LayoutParams);
    title.setTextColor(textcolor);
    title.setTextSize(20);
    title.setPadding(20, 15, 0, 0);
    head.addView(title);
    mView = new ImageView(c);
    image = new ImageView(mView.getContext());

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 20, 20, 10);
    image.setLayoutParams(layoutParams);
    image.setImageDrawable(mView.getResources().getDrawable(imageViewBackground));
    head.addView(image);
    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(c, "hhhhhh", Toast.LENGTH_SHORT).show();
            System.out.println("hhhhhhh");
        }
    });
    wrapper.addView(head);

    t = new TextView(c);
    if (msg.equals("")) {
        t.setVisibility(View.GONE);
    }else
    {
        t.setVisibility(View.VISIBLE);

    }

    t.setText("" + msg);
    t.setTypeface(face);

    t.setTextColor(textcolor);
    t.setTextSize(25);
    t.setPadding(20, 10, 0, 10);
    wrapper.addView(t);
    ly1.setOnTouchListener(lltouch);

    t1 = new TextView(c);
    t1.setText("" + contactName);
    t1.setTextColor(textcolor);
    t1.setTextSize(20);
    //        t1.setPadding(20, (int)  
     TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
     c.getResources().getDisplayMetrics()), (int)   
     TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0, 
      c.getResources().getDisplayMetrics()), (int)  
      TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,  
       c.getResources().getDisplayMetrics()));                                                 
    t1.setPadding(20, 0, 10, 10);
    wrapper.addView(t1);

    ly1.addView(wrapper);
    wm.addView(ly1, prms);

    return ly1;
}

我用

打电话给我
    Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable() {

    @Override
    public void run() {

        wm = (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);

        getWindowLayout();

            getManualLinearLayout();

    }

};

请告诉我如何解决此问题。

Attached Image

1 个答案:

答案 0 :(得分:1)

问题可能来自于您没有明确声明您将自己管理可聚焦性的事实。尝试将此添加到您的LinearLayout:

LinearLayout ln = new LinearLayout(context);
ln.setDescendantFocusability(ViewGroud.FOCUS_BLOCK_DESCENDANTS);

应该确保您可以管理布局中元素的所有可聚焦性(单击等)。