将视图添加到flexboxlayout

时间:2018-05-18 10:39:35

标签: android android-layout android-flexboxlayout

单击按钮,我以编程方式创建一个textview并将它们添加到flexboxlayout。为什么只添加了一个视图?

mButton.setOnClickListener(new View.OnClickListener() {
        int i = 1;
        @Override
        public void onClick(View v) {
            flexboxLayout.setFlexDirection(FlexDirection.ROW_REVERSE);
            flexboxLayout.setFlexWrap(FlexWrap.WRAP);
            flexboxLayout.setJustifyContent(JustifyContent.CENTER);
            final TextViewR textViewR = new TextViewR(getApplicationContext(), "Text " + Integer.toString(i++)); //setting text here
            FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.setOrder(1);
            textViewR.setLayoutParams(lp);
            flexboxLayout.addView(textViewR);
        }
    });

1 个答案:

答案 0 :(得分:0)

我认为TextView是您的自定义`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);`

你应该改变

`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);` 

textViewR.setLayoutParams(lp);

因为{{1}}正在将flexboxlayout参数设置为textview,并且您的参数告诉textview与父元素即flexbox匹配。