Android:用线连接按钮 [英] Android: Connect buttons with lines
问题描述
我正在编写一个与二叉树/图形非常相似的Android应用程序,有多个动态创建的按钮,但我不知道如何创建连接这些按钮的线.
I´m writing an Android Aplication that is very similar to a binary tree/graph, there are several buttons created dinamically but I can´t figure out how to create lines connecting the buttons.
以下是显示我需要做什么的图像:
Here´s an image showing what I need to do:
这是我的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);
}
}
请问,关于如何执行此操作的任何想法?
Please, any ideas on how can I do that?
推荐答案
您可能想说一下NodeLink在做什么-自定义视图类?
You might want to say what NodeLink is doing - custom view class?
无论如何,我建议您获取按钮上锚点"的坐标(从图表中获取顶部中心点),然后创建链接相关按钮锚点的路径.将这些路径绘制/绘制到图片中,将其另存为 PictureDrawable ,然后您可以将该Drawable设置为按钮的父级背景( RelativeLayout/mainLayout).
Any case, I'd suggest getting the coordinates of "anchors" on your buttons (top-center points, from your diagram), then creating Paths that link associated buttons' anchors. Draw/paint these Paths into a Picture, save it as a PictureDrawable, and then you can set that Drawable as the background of your buttons' parent (the RelativeLayout/mainLayout).
这篇关于Android:用线连接按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!