如何在Android布局中动态画线 [英] How to draw a line dynamically in Android layout
问题描述
我做了什么:
我在寻找什么
我不在乎设计,但是我不知道如何使用类似于第二个图像的线条将按钮连接到主按钮.注意:我正在动态创建按钮.因此,我不使用XML文件,因为我不知道我将拥有多少行/按钮.
受保护的void onCreate(捆绑保存的InstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.second_layout);//RelativeLayout FirstLayout =(RelativeLayout)findViewById(R.id.second_layou);RelativeLayout.LayoutParams parms1 =新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);按钮yourButton = new Button(this);yourButton.setText("1");yourButton.setWidth(2);parms1.setMargins(w,h + 250,0,0);FirstLayout.addView(yourButton,parms1);//RelativeLayout.LayoutParams parms2 =新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);Button yourButton2 =新的Button(this);yourButton2.setText("2");yourButton2.setWidth(2);parms2.setMargins(w-300,h + 300,0,0);FirstLayout.addView(yourButton2,parms2);//等等,还有其他按钮
修改:
第一:当我尝试.
< FrameLayout xmlns:android ="http://schemas.android.com/apk/res/android"android:layout_width ="match_parent"android:layout_height ="match_parent"android:id ="@ + id/root_layout">< com.dev.example.CustomDrawingViewandroid:id ="@ + id/custom_drawing_view"android:layout_width ="wrap_content"android:layout_height ="wrap_content"/></FrameLayout>
然后,我通过代码将 TextViews
动态添加到 FrameLayout
,并使用 Path
对象在 CustomDrawingView
,从每个 TextView
的中心到中心:
路径路径= new Path();int x1 =(int)firstView.getX()+ firstView.getWidth()/2;int y1 =(int)firstView.getY()+ firstView.getHeight()/2;int x2 =(int)secondView.getX()+ secondView.getWidth()/2;int y2 =(int)secondView.getY()+ secondView.getHeight()/2;path.moveTo(x1,y1);path.lineTo(x2,y2);
现在,曲线是根据以下问题得出的答案: Edit: First: When I tried this answer by adding this code at the end of a line view is shown but buttons are gone! and I want the line to be drawn from the center of Second: when I add the same code before defining the buttons I got runtime error, and this is logcat: I actually needed something similar and was bummed that there was no answer to this. After some searching and combining of information I got to this state: What I did was take a Then I add the Now the curve is made with the answer from this question: How to draw a curved line between 2 points on canvas? Simply adjust the Hope this helps someone in the future. EDIT: Oh and what's really important is to only call the 这篇关于如何在Android布局中动态画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!onCreate()
:drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
MAIN
button to center of other buttons not from fixed points.01-29 15:25:25.956 26170-26170/com.example.user.raywenderlich E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.user.raywenderlich, PID: 26170 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.raywenderlich/com.example.user.raywenderlich.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.user.raywenderlich.SecondActivity.onCreate(SecondActivity.java:46)
at android.app.Activity.performCreate(Activity.java:5958)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
FrameLayout
, so the TextViews
could be stacked on top of the drawing View
. I made a custom drawing View
as suggested in this tutorial: Basic Painting with Views. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root_layout">
<com.dev.example.CustomDrawingView
android:id="@+id/custom_drawing_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
TextViews
dynamically via code to the FrameLayout
and use Path
objects to draw the lines in the CustomDrawingView
, from center to center of each TextView
: Path path = new Path();
int x1 = (int) firstView.getX() + firstView.getWidth() / 2;
int y1 = (int) firstView.getY() + firstView.getHeight() / 2;
int x2 = (int) secondView.getX() + secondView.getWidth() / 2;
int y2 = (int) secondView.getY() + secondView.getHeight() / 2;
path.moveTo(x1, y1);
path.lineTo(x2, y2);
curveRadius
in that answer to have different looking curves.
onDraw
method of the CustomDrawingView
after all the layouts and views have been displayed. Because else the TextView
's getX() & getY()
methods still return 0 and you won't see any lines.
The suggested position to start drawing them was in the onWindowFocusChanged
method of an activity.