ViewGroup addview()在Android 7.0中无效

时间:2018-01-09 02:26:03

标签: view

customlayout通过addview()在Android 7.0中无效,布局消失。 低于7.0是成功的。但是我在Android 5.0,6.0,7.0中记录了这个布局的宽度和高度是0

2 个答案:

答案 0 :(得分:0)

您还应该设置LayoutParams。 例如:

LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
TextView tv=new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("this view is added dynamically");
mainLayout.addView(text);

答案 1 :(得分:0)

我设置了LayoutPrams:

public class Card extends FrameLayout {
...

    private void initial() {
        label = new TextView(getContext());
        label.setTextSize(32);
        label.setBackgroundColor(0x33ff0033);
        label.setTextColor(0x330D0D0D);
        label.setGravity(Gravity.CENTER);
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        lp.setMargins(10, 10, 0, 0);
        addView(label, lp);
        setNum(0);
    }
...
}

我设置:

public  NumLayout extends GridLayout{
...
private void addCard(int cardWidth, int cardHeight) {
        Card card;
        int sum = 0;
        for (int x = 0; x < NUMBER; x++) {
            for (int y = 0; y < NUMBER; y++) {
                card = new Card(getContext());
                card.setNum(number[sum]-1);
                addView(card, cardWidth, cardHeight);
                point[x][y] = card;
                sum++;
            }
        }
    }
...
}