gpt4 book ai didi

java - 如何在android中的按钮之间画线

转载 作者:行者123 更新时间:2023-11-30 11:17:00 26 4
gpt4 key购买 nike

我想在两个按钮的中点之间画线。我使用下面的代码创建了它,我在我的 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>

//主 Activity

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;
}

}
}

//绘图 View

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);
}

}

最佳答案

<View android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/white" />

这将创建一条 2dp 高的水平线。在你的两个按钮之间添加这个,并在你的 RelativeLayout

中适当定位

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

关于java - 如何在android中的按钮之间画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24599643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com