0

Ich habe ein Problem in meinem benutzerdefinierten Layout, wie Höhe auf dem grauen TextView gesetzt wird, damit es genauso aussieht wie gelb TextView. Gelbe TextView erlauben mehrzeilige aber graue Unterstützung nur einzelne Zeile. Unter meinem benutzerdefinierten Layout. P.S. Ich setze gerundeten Hintergrund direkt auf TextView Thx.Wie höhe zwei TextView im benutzerdefinierten Layout ausrichten?

enter image description here

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); 
    } 
} 

Antwort

0

genaue Höhe einer Ansicht zur Laufzeit zu bekommen, besser mit

view.post(new Runnable() { 

     @Override 
     public void run() { 
      view.getLayoutParams().height; 
     } 

}); 

Sie diese Höhe verwenden können, für andere zu setzen LayoutParams verwenden.

Verwandte Themen