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

3
我希望将一些视图(主要是TextView)添加到LinearLayout中,但它只能在某些设备上工作。我已经在三星Galaxy S5、联想Tab2和三星Galaxy S9上测试过这段代码。只有S5可以使用此代码并将视图添加到LinearLayout中,其他设备无法添加。这段代码有什么问题?xml代码有问题吗?
提前致谢。
Java代码:
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;
    }

XML 代码:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="?attr/actionBarSize"
    android:paddingBottom="8dp">

        <LinearLayout
            android:id="@+id/layContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="8dp"
            android:orientation="vertical"
            android:paddingBottom="12dp">

        </LinearLayout>
</android.support.v4.widget.NestedScrollView>

1
请设置TextView的布局参数。textView.setLayoutParams(new LayoutParams(LayoutParams - jay shah
首先将 layContainer 的高度改为 wrap_content。并且为 textView 设置布局参数和 gravity。 - Piyush
@ jay shah 谢谢,我还没有考虑过这个。 - Mohsen Hatami
1个回答

1

您需要为创建的TextView设置布局参数。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接