3

我做了什么:

图1

我在找什么:

图 2

我不关心设计,但我不知道如何使用类似于第二张图像的线条将按钮连接到主按钮。注意:我正在动态创建按钮。因此,我不使用 XML 文件,因为我不知道我将拥有多少行/按钮。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_layout);
    //
    RelativeLayout FirstLayout = (RelativeLayout)findViewById(R.id.second_layou);
    RelativeLayout.LayoutParams parms1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button yourButton = new Button(this);
    yourButton.setText("1");
    yourButton.setWidth(2);
    parms1.setMargins(w, h+250, 0, 0);
    FirstLayout.addView(yourButton, parms1);
    //
    RelativeLayout.LayoutParams parms2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button yourButton2 = new Button(this);
    yourButton2.setText("2");
    yourButton2.setWidth(2);
    parms2.setMargins(w-300, h+300, 0, 0);
    FirstLayout.addView(yourButton2, parms2);
    // and so on with other buttons

编辑

首先:当我通过在末尾添加此代码来尝试onCreate()答案时:

drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

显示了一个线视图,但按钮不见了!我希望这条线从MAIN按钮的中心绘制到其他按钮的中心,而不是从固定点。

第二:当我在定义按钮之前添加相同的代码时,出现运行时错误,这是 logcat:

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)

 

4

1 回答 1

2

我实际上需要类似的东西,并且对没有答案感到沮丧。

经过一些搜索和信息组合后,我达到了这个状态:

一些由曲线连接的文本视图。


我所做的是取一个FrameLayout,所以TextViews可以堆叠在图纸的顶部ViewView我按照本教程中的建议制作了一个自定义绘图: 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动态通过代码添加到FrameLayout并使用Path对象在 中绘制线条CustomDrawingView,从每个中心到中心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);

现在曲线是用这个问题的答案制作的:如何在画布上的 2 点之间绘制曲线?

只需调整该curveRadius答案中的 即可获得不同的外观曲线。

希望这对将来的某人有所帮助。


编辑:哦,真正重要的是在显示所有布局和视图之后才调用onDraw方法。因为 else的方法仍然返回 0 并且你不会看到任何行。 开始绘制它们的建议位置是在活动的方法中。CustomDrawingView TextViewgetX() & getY()
onWindowFocusChanged

于 2019-04-08T17:56:23.573 回答