【问题标题】:how to draw line between buttons in android如何在android中的按钮之间画线
【发布时间】:2019-09-18 21:15:34
【问题描述】:

我想在两个按钮的中间点之间画线。我创建了使用下面的代码,我在我的 android 模拟器中检查它是否正常工作。我把那个 apk 放到我的手机上,但这些线的位置不正确。
如何定位两个按钮的两个中点之间的线?
它应该适用于每个具有不同屏幕尺寸的安卓手机
这就是我想画线的方式 ![IMG]http://i58.tinypic.com/1o7hj9.png[/IMG] [![安卓模拟器]1]1

//activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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" >

    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffaa" >
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:id="@+id/linearLayout1">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <Button
        android:layout_weight="1"
        android:id="@+id/bx1y1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="" />
    <Button
        android:layout_weight="1"
        android:id="@+id/bx2y1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>
<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    <Button
        android:layout_weight="1"
        android:id="@+id/bx1y2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="" />
    <Button
        android:layout_weight="1"
        android:id="@+id/bx2y2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>
</LinearLayout>
</RelativeLayout>

//主活动

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.graphics.Color;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

      DrawView drawView;

      Button bx1y1,bx2y1,bx1y2,bx2y2;
      RelativeLayout rl1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

         bx1y1 = (Button) findViewById(R.id.bx1y1);
         bx2y1 = (Button) findViewById(R.id.bx2y1);
         bx1y2 = (Button) findViewById(R.id.bx1y2);
         bx2y2 = (Button) findViewById(R.id.bx2y2);
         rl1 =(RelativeLayout)findViewById(R.id.rl1);


         bx2y1.setOnClickListener(new View.OnClickListener() {  
                @Override
                public void onClick(View arg0) {
                    drawline(1);
                }
            });

         bx1y2.setOnClickListener(new View.OnClickListener() {  
                @Override
                public void onClick(View arg0) {
                    drawline(3);
                    drawline(6);
                }
            });

         bx2y2.setOnClickListener(new View.OnClickListener() {  
                @Override
                public void onClick(View arg0) {
                    drawline(2);
                    drawline(4);

                    drawline(5);
                }
            });
        }
        protected void drawline(int linenum) {
            switch (linenum) {
            case 1://x start
                drawView = new DrawView(MainActivity.this,bx1y1,bx2y1,18,18,18,18);
                  rl1.addView(drawView);
                break;
            case 2:
                drawView = new DrawView(MainActivity.this,bx1y2,bx2y2,18,54,18,54);
                  rl1.addView(drawView);
                break;
            case 3:
                drawView = new DrawView(MainActivity.this,bx1y1,bx1y2,18,18,18,54);
                  rl1.addView(drawView);
                break;
            case 4:
                drawView = new DrawView(MainActivity.this,bx2y1,bx2y2,18,18,18,54);
                rl1.addView(drawView);
                break;
            case 5:
                drawView = new DrawView(MainActivity.this,bx1y1,bx2y2,18,18,18,54);
                rl1.addView(drawView);
                break;
            case 6:
                drawView = new DrawView(MainActivity.this,bx2y1,bx1y2,18,18,18,54);
                rl1.addView(drawView);
                break;

            default:
                break;
            }

        }
}

//绘图视图

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();
    View startView;
    View endView;
    int sx,sy,ex,ey;

    public DrawView(Context context,View startView,View endView,int sx,int sy,int ex,int ey) {
        super(context);
        paint.setColor(Color.BLACK);        
        this.startView = startView;
        this.endView = endView;
        this.sx=sx;
        this.sy=sy;
        this.ex=ex;
        this.ey=ey;
    }

    @SuppressLint("NewApi")
    public void onDraw(Canvas canvas) {
            canvas.drawLine(startView.getX()+sx, startView.getY()+sy, endView.getX()+ex, endView.getY()+ey, paint);
    }

}

【问题讨论】:

    标签: java android


    【解决方案1】:
    <View android:layout_width="match_parent"
      android:layout_height="2dp"
      android:background="@android:color/white" />  
    

    这将创建一条高度为2dp 的水平线。在你的两个按钮之间添加这个,在你的RelativeLayout 中适当定位

    如果您希望在两个按钮之间有一条更加自定义的线条,另一种选择是使用可绘制的形状。

    【讨论】:

    • 实际上我想在两个按钮的中间点之间画线我如何定位它?它应该适用于每部安卓手机(不同的显示尺寸)
    • 查看FrameLayout。看看有没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多