我的自定义布局中存在如何在灰色TextView
上设置高度以使其看起来与黄色TextView
相同的问题。黄色TextView
允许多行但灰色仅支持单行。在我的自定义布局下面
附:我直接在TextView
上设置了圆角背景
THX。
public class LayoutSkillChips extends ViewGroup {
private TextView mSkillName;
private TextView mSkillCounter;
public LayoutSkillChips(Context context) {
super(context);
initialize(context);
}
public LayoutSkillChips(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}
public LayoutSkillChips(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context context) {
LayoutInflater.from(context).inflate(R.layout.layout_skill_chips, this, true);
mSkillName = (TextView) findViewById(R.id.skill_name);
mSkillCounter = (TextView) findViewById(R.id.skill_counter);
}
private void layoutView(View view, int left, int top, int width, int height) {
MarginLayoutParams margins = (MarginLayoutParams) view.getLayoutParams();
final int leftWithMargins = left + margins.leftMargin;
final int topWithMargins = top + margins.topMargin;
view.layout(leftWithMargins, topWithMargins,
leftWithMargins + width, topWithMargins + height);
}
private int getWidthWithMargins(View child) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
return child.getWidth() + lp.leftMargin + lp.rightMargin;
}
private int getHeightWithMargins(View child) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
return child.getHeight() + lp.topMargin + lp.bottomMargin;
}
private int getMeasuredWidthWithMargins(View child) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
return child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
}
private int getMeasuredHeightWithMargins(View child) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
return child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int widthUsed = 0;
int heightUsed = 0;
measureChildWithMargins(mSkillCounter, widthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
widthUsed += getMeasuredWidthWithMargins(mSkillCounter);
measureChildWithMargins(mSkillName, widthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
heightUsed += getMeasuredHeightWithMargins(mSkillName);
int heightSize = heightUsed + getPaddingTop() + getPaddingBottom();
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();
int contentLeft = paddingLeft;
layoutView(mSkillName, contentLeft, paddingTop, mSkillName.getMeasuredWidth(), mSkillName.getMeasuredHeight());
contentLeft += getMeasuredWidthWithMargins(mSkillName);
layoutView(mSkillCounter, contentLeft, paddingTop, mSkillCounter.getMeasuredWidth(), mSkillCounter.getMeasuredHeight());
}
@Override
public boolean shouldDelayChildPressedState() {
return false;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
}
答案 0 :(得分:0)
在运行时获取视图的精确高度, 更好地使用
view.post(new Runnable() {
@Override
public void run() {
view.getLayoutParams().height;
}
});
您可以使用该高度为其他人使用LayoutParams设置。