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

littlezhy littlezhy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我自定义了 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);

}
});

}
...全文
评论 2
收藏 已收藏
分享
返回顶部
littlezhy
Java 51,407 成员 86,137 内容