Android的无视我setWidth(),并自动调用setHeight() [英] Android ignoring my setWidth() and setHeight()
本文介绍了Android的无视我setWidth(),并自动调用setHeight()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
那么,为什么这code:
So, why does this code:
package org.popoffka.apicross;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class Game extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button testButton = new Button(this);
testButton.setBackgroundResource(R.drawable.cell);
testButton.setWidth(20);
testButton.setHeight(20);
setContentView(testButton);
}
}
...产生这样的事情:
http://i42.tinypic.com/2hgdzme.png 即使有一个setWidth(20)和自动调用setHeight(20)在code?
(R.drawable.cell实际上是包含了银色边框的白细胞20×PNG图片)
...produce this thing: http://i42.tinypic.com/2hgdzme.png even though there's a setWidth(20) and setHeight(20) in the code? (R.drawable.cell is actually a 20x20 PNG image containing a white cell with a silver border)
推荐答案
要设置视图的宽度和高度,正确的方法是通过的LayoutParams这样做。见ViewGroup.LayoutParams和View.getLayoutParams()。
The proper way to set the width and height of a View is to do so via the LayoutParams. See ViewGroup.LayoutParams and View.getLayoutParams().
这篇关于Android的无视我setWidth(),并自动调用setHeight()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文