airbnb / paris

Define and apply styles to Android views programmatically

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

set Margin not working when add View in LinearLayout

THEONE10211024 opened this issue · comments

here is the code:
`public class MainActivity extends AppCompatActivity {
private LinearLayout mContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContainer = findViewById(R.id.ll_container);

}

private Style provideStyles(TextView textView) {
    Style myStyle = Paris.styleBuilder(textView)
            .add(R.style.normalNegativeButton)
            .layoutMarginStartRes(R.dimen.xn10)
            .layoutWidthRes(R.dimen.xn120)
            .layoutHeightRes(R.dimen.yn40)
            .textSizeRes(R.dimen.xn16)
            .build();
    return myStyle;
}

private List<TextView> getOperatorViews() {
    Style style = null;
    List<TextView> result = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        TextView textView = new TextView(this);
        if(style == null){
            style = provideStyles(textView);
        }
        Paris.style(textView).apply(style);
        result.add(textView);
    }
    return result;
}

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    mContainer.removeAllViews();
    List<TextView> re = getOperatorViews();
    for(TextView view : re){
        view.setText("text");
        mContainer.addView(view);
    }
}

}`

reason:
LinearLayout receives LinearLayout.LayoutParams while Paris generates ViewGroup.MarginLayoutParams, check the code "protected boolean checkLayoutParams(ViewGroup.LayoutParams p)" in LinearLayout

@THEONE10211024 what is the issue exactly? Is an exception thrown? Or is the margin simply not applied?

margin simply not applied,and I found out the solution:
View divider = new View(getContext());
divider.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));//add this line ,and margin will work
Paris.style(divider).apply(dividerStyle);
parent.addView(divider);