1
我是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>
我想显示我的活动里面几个矩形。为此,我创建了一个扩展View的新类。我实例化了这个类,并设法在我的Activity中显示我的矩形(也许我应该在这里使用word布局,我不确定...)。
我创建的类:
public class DrawRectangle extends View {
Paint paint = new Paint();
int x,y;
int length,width;
public DrawRectangle(Context context,int _x,int _y, int _length, int _width){
super(context);
x = _x;
y = _y;
length = _length;
width = _width;
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawRect(x, y, x+length, y+length, paint);
}
}
而且创造DrawRectangle的类的实例的活动里面的功能:
public void drawRectangle(){
DrawRectangle rectangle = new DrawRectangle(this,100,100,50,50);
// rectangle.setBackgroundColor(Color.TRANSPARENT);
setContentView(rectangle);
}
事情是这样的:
- 矩形已被正确绘制
- 我们看不到textview了
我试图创建DrawRectangle类的另一个实例,只有一个可以看到(最后一个在屏幕上设置)。我认为这个问题来自这个事实,即这个观点占据了所有的位置,并且放在了所有事物的前面。于是,我就背景颜色改变为透明使用线路:
rectangle.setBackgroundColor(Color.TRANSPARENT);
或
rectangle.setBackgroundColor(Color.parseColor("#00000000");
有没有任何错误,但其他矩形或文本视图是看不见的。
我该怎么办?我怀疑这是一个错误的理解。
谢谢你们!
Thx for answering。我怎样才能知道我的布局ID?关于findViewByID(R.id.your_layout_id)。 – Xema 2014-10-20 21:21:21
这是布局xml文件的名称。 – JoaoBiriba 2014-10-20 21:44:15
我的布局没有任何ID,所以这就是为什么我有点迷路。现在它工作了!谢谢 ! – Xema 2014-10-21 20:04:41