Android 在活动内绘制多个视图
我是Android编程新手,我有一些关于绘图的基本问题 我有一个活动通过.xml文件显示textview,如下所示: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">
<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);
}
事情是这样的:
- 矩形已正确绘制
- 我们再也看不到文本视图了
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);