如何在Android中的另一个自定义视图中添加自定义视图?

时间:2015-01-22 14:04:08

标签: android android-custom-view

我想创建一个自定义视图,应该在另一个自定义视图中添加。

第二个视图将是一个容器,因此它应该能够包含第一个视图作为其子视图。

为了创建这些观点,我正在扩展ViewGroup& LinearLayout课程。

子视图类是 NodeView

public class NodeView extends LinearLayout
{
    private final static String TAG = "NodeView";
    private ImageView ivTop;
    private ImageView ivBottom;
    private Context myContext;

    public NodeView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.myContext = context;

        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_test_multi, this, true);

        ivTop = (ImageView) getChildAt(0);
        ivBottom = (ImageView) getChildAt(2);

        ivTop.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(myContext, "Top Clicked", Toast.LENGTH_SHORT).show();
            }
        });

        ivBottom.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(myContext, "Bottom Clicked", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
    }

    public NodeView(Context context)
    {
        this(context, null);
    }
}

&安培;容器类是 TreeViewGroup

public class TreeViewGroup extends ViewGroup
{
private static final String TAG = "CustomTreeNodeView";

NodeView nodeView;

public TreeViewGroup(Context context, AttributeSet attrs, int defStyleAttr)
{
    super(context, attrs, defStyleAttr);
    nodeView = new NodeView(getContext());
    addView(nodeView);
}

public TreeViewGroup(Context context, AttributeSet attrs)
{
    this(context, attrs, 0);
}

public TreeViewGroup(Context context)
{
    this(context, null, 0);
}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
  }
}

&安培;节点视图的xml布局为 view_test_multi.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >


    <ImageView
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_centerVertical="true"
        android:src="@drawable/point_grey" />

    <ImageView
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_centerVertical="true"
        android:src="@drawable/point_red" />

    <ImageView
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_centerVertical="true"
        android:src="@drawable/point_grey" />

</merge>

我的活动布局为 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.ab1209.testcustom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.ab1209.testcustom.view.TreeViewGroup
        android:id="@+id/activity_main_custom_tree_node_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

MainActivity 类是

**public class MainActivity extends Activity
{

    TreeViewGroup treeNodeView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        treeNodeView = (TreeViewGroup) findViewById(R.id.activity_main_custom_tree_node_view);
    }

}**

当我运行应用程序时,我看不到主视图中添加了NodeView。我是否做对了,如果没有,请告诉我如何才能让它发挥作用?

1 个答案:

答案 0 :(得分:1)

  

要创建自定义ViewGroup,您需要覆盖的唯一方法是   onLayout。 onLayout在ViewGroup本身之后触发   完成了自己的容器ViewGroup内部的自我完成   现在负责安排孩子。它应该调用布局   所有孩子的方法现在定位和调整它们(左边   和top参数将决定子视图的x和y以及   右侧和底部将确定其宽度(右 - 左)和高度   (顶部 - 底部)。

因此,您的TreeViewGroup代码将如下所示:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {

                ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) child
                        .getLayoutParams();

                int childLeft = 0;
                int childTop = 0;
                child.layout(childLeft, childTop,
                        childLeft + child.getMeasuredWidth(),
                        childTop + child.getMeasuredHeight());

            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int measuredWidth = 200; // Calculate the height
        int measuredHeight = 200; // Calculate the width

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

请参阅此链接http://arpitonline.com/2012/07/01/creating-custom-layouts-for-android/