Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在活动内绘制多个视图_Android_View_Draw - Fatal编程技术网

Android 在活动内绘制多个视图

Android 在活动内绘制多个视图,android,view,draw,Android,View,Draw,我是Android编程新手,我有一些关于绘图的基本问题 我有一个活动通过.xml文件显示textview,如下所示: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

我是Android编程新手,我有一些关于绘图的基本问题

我有一个活动通过.xml文件显示textview,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:textSize="18dp"
        android:textStyle="bold" />

</RelativeLayout>
以及创建DrawRectangle类实例的活动中的函数:

public void drawRectangle(){

    DrawRectangle rectangle = new DrawRectangle(this,100,100,50,50);
    // rectangle.setBackgroundColor(Color.TRANSPARENT);
    setContentView(rectangle);
}
事情是这样的:

  • 矩形已正确绘制
  • 我们再也看不到文本视图了
我试图创建DrawRectangle类的另一个实例,但只能看到一个(最后一个在屏幕上设置)。我认为问题的根源在于,视图占据了所有的位置,并被置于所有事物的前面。因此,我尝试使用以下行将背景色更改为透明:

rectangle.setBackgroundColor(Color.TRANSPARENT);

没有任何错误,但无法看到其他矩形或文本视图

我能做什么?我怀疑对这一切没有理解


谢谢大家

setContentView
中,必须传递活动的xml布局(包含文本视图)。 稍后,您可以将其他视图添加到此布局。例如:

ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);

谢谢回答。如何获取/知道布局ID?关于findViewByID(R.id.your_layout_id)。它是布局xml文件的名称。我的布局没有任何id,因此我有点迷路。现在它开始工作了!谢谢!
rectangle.setBackgroundColor(Color.parseColor("#00000000");
ViewGroup layout = (ViewGroup) findViewById(R.id.your_layout_id);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("Added tv");
layout.addView(tv);