问题描述
我在什么时候使用setWidth,并自动调用setHeight混淆?它通常不工作。
I am confused on when to use setWidth, and setHeight? It usually don't work.
什么总是工作是setLayoutParams。
What always work is setLayoutParams.
这会工作。
sampleButton = new Button(this);
sampleButton.setLayoutParams(new LinearLayout.LayoutParams(65, 65));
这是行不通的。
sampleButton = new Button(this);
sampleButton.setHeight(65);
sampleButton.setWidth(65);
或者,也许有一些初始化这个code的工作?
Or maybe there are some initialisation for this code to work?
推荐答案
所以,只是看着按钮的
源$ C $ C(这是一个子类的TextView
)
So, just looked at the Button
's source code (which is a subclass of TextView
):
<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.1_r2/android/widget/TextView.java#TextView.setHeight%28int%29\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.1_r2/android/widget/TextView.java#TextView.setHeight%28int%29
这里是自动调用setHeight方法
here is the method for setHeight
public void setHeight(int pixels) {
mMaximum = mMinimum = pixels;
mMaxMode = mMinMode = PIXELS;
requestLayout();
invalidate();
}
目前在 onMeasure
方法 mMaximum
和 mMaxMode
这里使用
int unpaddedHeight = height - getCompoundPaddingTop() - getCompoundPaddingBottom();
if (mMaxMode == LINES && mLayout.getLineCount() > mMaximum) {
unpaddedHeight = Math.min(unpaddedHeight, mLayout.getLineTop(mMaximum));
}
它看起来像自动调用setHeight并没有真正覆盖内部高度参数,更多的只是设置标志的布局。
It looks like the setheight doesn't really overwrite the internal height parameters, more just sets flags for layout.
更改布局PARAMS(这是当视图铺设本身出了什么实际参考)似乎告诉它实际上需要是HIGHT
Changing the layout params (which are what are actually referenced when the view is laying itself out) seem to inform the view that it actually needs to be that hight
TLDR;自动调用setHeight更多的是与文本不是视图的高度
这篇关于何时使用setWidth,自动调用setHeight,setLayoutParams(新LinearLayout.LayoutParams(W,H))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!