Android:用线条连接按钮

时间:2012-08-12 21:06:00

标签: android android-layout android-widget

我正在编写一个非常类似于二叉树/图形的Android应用程序,有几个按钮创建的按钮,但我无法弄清楚如何创建连接按钮的行。

这是一张图片,展示了我需要做的事情:

这是我的XML文件:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vscroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000" >

    <HorizontalScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hscroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#888888" >

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/relativescroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#FFFFFF" >

            <!-- Buttons and lines connecting them -->

        </RelativeLayout>
    </HorizontalScrollView>

</ScrollView>

以下是活动类的示例:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.relativescroll);
        RelativeLayout.LayoutParams newParams;

        // Botão 1
        Button btn1 = new Button(this); 
        btn1.setId(1);
        btn1.setText("Botão 1");
        newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        newParams.setMargins(0, 0, 20, 50);
        newParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        btn1.setLayoutParams(newParams);
        mainLayout.addView(btn1);

        // Botão 2
        Button btn2 = new Button(this);
        btn2.setId(2);
        btn2.setText("Botão 2");
        newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        newParams.setMargins(0, 0, 20, 50);
        newParams.addRule(RelativeLayout.BELOW, 1);
        newParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        btn2.setLayoutParams(newParams);
        mainLayout.addView(btn2); 

        // Botão 3
        Button btn3 = new Button(this);
        btn3.setId(3);
        btn3.setText("Botão 3");
        newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        newParams.setMargins(0, 0, 20, 50);
        newParams.addRule(RelativeLayout.BELOW, 1);
        newParams.addRule(RelativeLayout.RIGHT_OF, 2);
        btn3.setLayoutParams(newParams);
        mainLayout.addView(btn3);

        // Botão 4
        Button btn4 = new Button(this);
        btn4.setId(4);
        btn4.setText("Botão 4");
        newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        newParams.setMargins(0, 0, 20, 50);
        newParams.addRule(RelativeLayout.BELOW, 1);
        newParams.addRule(RelativeLayout.RIGHT_OF, 3);
        btn4.setLayoutParams(newParams);
        mainLayout.addView(btn4);

        // Botão 4
        Button btn5 = new Button(this);
        btn5.setId(5);
        btn5.setText("Botão 5");
        newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        newParams.setMargins(0, 0, 20, 50);
        newParams.addRule(RelativeLayout.BELOW, 1);
        newParams.addRule(RelativeLayout.RIGHT_OF, 4);
        btn5.setLayoutParams(newParams);
        mainLayout.addView(btn5);   

        // DRAW LINK        
        NodeLink link = new NodeLink(this);
        newParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        newParams.addRule(RelativeLayout.BELOW, 1);
        newParams.addRule(RelativeLayout.ABOVE, 2);
        newParams.addRule(RelativeLayout.RIGHT_OF, 1);
        newParams.addRule(RelativeLayout.LEFT_OF, 2);
        mainLayout.addView(link);

    }

}

请问,我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可能想说明NodeLink正在做什么 - 自定义视图类?

无论如何,我建议在你的按钮上获得“锚点”的坐标(从图中的顶部中心点),然后创建链接相关按钮锚点的路径。将这些路径绘制/绘制到Picture,将其另存为PictureDrawable,然后将该Drawable设置为background of your buttons' parent(RelativeLayout / mainLayout)。