【发布时间】:2020-06-14 05:07:45
【问题描述】:
我在同一个“页面”上有一个按钮和一个 ImageView。现在,我创建了一个 OnClickListener,当我单击按钮时,页面会清除并且我的绘图会出现。我想我必须改变 setContentView(meinView) 但我不知道怎么做? 我希望当我单击按钮时,将在同一“页面”上的 ImageView 中进行绘图。我是 android-programming 的新手,我不确定 xml-File 是否正确。它是由 Android Studio 3.5.3 制作的。
XML 文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="422dp"
android:layout_height="452dp"
android:layout_marginBottom="296dp"
android:background="#555555"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="253dp"
android:layout_marginTop="166dp"
android:onClick="BeimKlick"
android:text="Klick mich" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="109dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="6dp"
android:elevation="2dp"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="514dp" />
</RelativeLayout>
我的主要活动:
MeinView meinView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
meinView = new MeinView(this);
meinView.setBackgroundColor(Color.DKGRAY);
setContentView(R.layout.activity_main);
final Button button1 = findViewById(R.id.button1);
final ProgressBar progressbar1 = findViewById(R.id.progressBar1);
final ImageView iView = findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(meinView);
//setContentView(R.id.imageView);
}
});
}
这是我的绘图类:
int x;
for (x=0;x<=200;x+=5)
{
canvas.drawLine(x, 0, x, 1000, paint);
}
int y;
for (y=0;y<=200;y+=5)
{
canvas.drawLine(0, y, 1000, y, paint);
}
canvas.scale(6, 6);
paint.setColor(Color.RED);
canvas.drawText("Test", 20, 50, paint);
canvas.drawText("Row1", 20, 70, paint);
canvas.drawText("Row2", 20, 90, paint);
好的,我使用 Framelayout 而不是 Relativelayout,但结果与以前相同。控件(按钮/进度条等)消失并且可以看到绘图。我还更改了 SurfaceView 中的 ImageView 但结果相同(因为我认为 ImageView 可能不再是最新的了)
【问题讨论】:
标签: android button layout imageview