LinearLayout无法在某些设备上添加视图

时间:2018-10-17 06:32:04

标签: android android-linearlayout

我想向LinearLayout添加一些视图(主要是TextView),但它仅在某些设备上有效。我已经在三星Galaxy S5,联想Tab2和三星Galaxy S9上测试了此代码。仅S5使用此代码,它可以将视图添加到LinearLayout。其他的不能添加。此代码有什么问题? xml代码有问题吗?

预先感谢

Java代码:

fast_message

xml代码:

lay.addView(getContentView(this, "Hello", Color.RED));
,,,
,,,
public TextView getContentView(Context mContext, String str, int color) {
        Calendar calendar = Calendar.getInstance();
        int h = calendar.get(Calendar.HOUR_OF_DAY);
        int m = calendar.get(Calendar.MINUTE);
        int s = calendar.get(Calendar.SECOND);
        String time = (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" +  (s < 10 ? "0" + s : s);
        TextView textView = new TextView(mContext);
        textView.setText(time + " " + str);
        textView.setTextColor(color);
        return textView;
    }

1 个答案:

答案 0 :(得分:1)

您需要将布局参数设置为您创建的textview

textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));