【问题标题】:How to have a Viewgroup containing multiple ImageButton in android?如何在 android 中拥有一个包含多个 ImageButton 的 Viewgroup?
【发布时间】:2011-09-21 08:22:42
【问题描述】:

在创建自定义 Viewgroup 时,是否可以在其中包含多个 Imagebutton? 是否可以将图像按钮放置在我们自己的位置。

如果所有这些都是可能的,如何调用扩展这个视图组的点击监听器?

我的 ViewGroup 必须看起来像这张图片

编辑 1:

public class CustomViewGroup extends ViewGroup{

    public CustomViewGroup(Context context) {
        super(context);
        setWillNotDraw(false);
        //addImageView();
        // TODO Auto-generated constructor stub
    }
    public CustomViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        //addImageView();
        // TODO Auto-generated constructor stub
    }
    public CustomViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, 
                defStyle);
        setWillNotDraw(false);
        //addImageView();
        // TODO Auto-generated constructor stub
    }
    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        // TODO Auto-generated method stub
        System.out.println("onLayout");
    }
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        drawBackground(canvas);
        addImageView();
        //super.onDraw(canvas);
    }

    private void drawBackground(Canvas canvas)
    {
        Bitmap background =BitmapFactory.decodeResource(getContext().getResources(), R.drawable.shape_quarter_circle);
        canvas.drawBitmap(background, 0,0, null);
    }
    private void addImageView()
    {
        ImageView imageOne = new ImageView(getContext());
        imageOne.setImageDrawable(getContext().getResources().getDrawable(R.drawable.round_icon));
        //imageOne.setWillNotDraw(false);
        addView(imageOne);
        requestLayout();

    }

我正在尝试绘制背景并在背景顶部放置一些 ImageView。 在此代码中,背景图像正在正确显示。但我看不到在其上绘制的 ImageView。 我走的是正确的道路吗?

【问题讨论】:

    标签: android view custom-component viewgroup clicklistener


    【解决方案1】:

    你说的都是可能的。

    你只是使用

    yourViewGroup.setOnClickListener()
    

    为您的视图组单击侦听器。

    【讨论】:

    • 我已经用 viewgroup 扩展了 myclass。但我无法在其上放置任何 imageView。我应该在哪里以及如何添加 imageView?
    • Yashwanth Kumar,但这只会为我的整个 ViewGroup 创建 clickListener 而不会为单个图像创建,对吗?
    • 您应该查看 ViewGroup 的其中一个 android 源代码并查看如何将子项添加到其中,您对自定义 viewGroup 执行相同操作并为子项设置 onClickListeners,同时将它们添加到父视图组。
    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 2015-01-07
    • 2016-11-18
    • 1970-01-01
    相关资源
    最近更新 更多