自定义View组件

============问题描述============


我的自定义组件:public class MyView extends View{



public MyView(Context context) {
super(context);
// TODO 自动生成的构造函数存根
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
Paint p=new Paint();
canvas.drawRect(0, 100, 480, 800, p);

}
我的xml文件: 用的是TableLayout布局
        <com.example.exercise_shader.MyView
            android:id="@+id/myView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>


我的主函数:package com.example.exercise_shader;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;

public class MainActivity extends Activity implements OnClickListener{

private Button bn1;
private Button bn2;
private Button bn3;
private Button bn4;
private Button bn5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn1=(Button)findViewById(R.id.bn1);
        bn2=(Button)findViewById(R.id.bn2);
        bn3=(Button)findViewById(R.id.bn3);
        bn4=(Button)findViewById(R.id.bn4);
        bn5=(Button)findViewById(R.id.bn5);
        MyView myView=(MyView)findViewById(R.id.myView);

这样写模拟器为啥出不来效果,一直报错啊???
        
           

============解决方案1============


报什么错,把Log贴出来看看。

============解决方案2============


你的 layout 布局文件是 wrap_content, 而你又没有重写 onMeasue方法,所以 android 默认你的宽高为0,不进行绘制

============解决方案3============


主布局文件呢?发出来看看

============解决方案4============


画笔没有选颜色,当然没有效果。

Paint p=new Paint();

p.setColor(Color.BLUE); // <== 要选择一种颜色

canvas.drawRect(0, 100, 480, 800, p);

============解决方案5============


引用 6 楼 qianhaojie 的回复:
这是我的布局文件
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    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" >

    <TableRow 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button 
            android:id="@+id/bn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:text="位图"/>
        <Button 
            android:id="@+id/bn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:text="线性渐变"/>        
        <Button 
            android:id="@+id/bn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:text="圆形渐变"/>
        <Button 
            android:id="@+id/bn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:text="角度渐变"/>
        <Button 
            android:id="@+id/bn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"
            android:text="混合"/>                        
    </TableRow>
    
      
        <com.example.exercise_shader.MyView
            android:id="@+id/myView"
            android:layout_width="480dp"
            android:layout_height="700dp"/>
           

    

</TableLayout>


你的TableRow占据了整个TableLayout,怎么能看到自定义布局被挤掉了,你可以把自定义布局先放到上面去看看效果,然后再更改布局,实现你想要的效果

============解决方案6============


报错的信息贴出来看看

============解决方案7============


少年,没log 别人怎么给你看
posted @ 2014-10-17 10:25  android教程  阅读(188)  评论(0编辑  收藏  举报
我要啦免费统计