android 自定义view 怎么设置其宽度和高度

littlezhy 2012-01-18 06:08:16
我自定义了 myview extends SurfaceView implements Callback
在oncreat中引用自定义的view view的参数设置是这样的。
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);

但是这个view 总会占满父屏幕
下面是图:

那个灰色的就是自定义的view,我想把view的 宽度 和 高度 可以自己定义

那个 params 的 参数 WRAP_CONTENT 是不能改变的,改变了对我来说就没有意义了。

public class MyView04 extends SurfaceView implements Callback {

private SurfaceHolder sfh;
private boolean drawFlag = true;
private Canvas canvas;

public MyView04(Context context) {
super(context);
init();
}

public MyView04(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
sfh = this.getHolder(); // 备注1
sfh.addCallback(this);
}


private Thread th = new Thread() {
public void run() {
while (drawFlag) {
draw();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
};

public void draw() {
canvas = sfh.lockCanvas();
System.out.println("draw ..............");
Paint paint = new Paint();
paint.setTextSize(12);
canvas.drawColor(Color.GRAY);
canvas.drawText("asdfasdfsa", 0, 0, paint);
canvas.drawRect(0, 0, 2, 2, paint);
sfh.unlockCanvasAndPost(canvas);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
th.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
drawFlag = false;
}




-------------------------------这是主Activity--------------的代码


public class Main extends Activity {
private MyView04 myview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
Button btn01 = new Button(this);
btn01.setText("Button");
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
btn01.setLayoutParams(params);
layout.addView(btn01);



myview = new MyView04(this);
myview.setLayoutParams(params);

layout.addView(myview);

final Button btn02 = new Button(this);
btn02.setText("Button");
btn02.setLayoutParams(params);
System.out.println("addview btn02..............................");
layout.addView(btn02);

setContentView(layout,layoutParams);



btn01.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int width = myview.getWidth();
int height = myview.getHeight();

int width1 = btn02.getWidth();
int height1 = btn02.getHeight();


System.out.println(" myview widht is "+width + " myview height is "+ height);
System.out.println(" myview widht is "+width1 + " myview height is "+ height1);

}
});

}
...全文
7180 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
littlezhy 2012-01-30
  • 打赏
  • 举报
回复
谢谢,不过这个没用。
he_x 2012-01-24
  • 打赏
  • 举报
回复
try this:
MyView04.setLayoutParams(new LayoutParams(width, height));

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧