android - LinearLayout 无法在某些设备上添加视图
问题描述
我想向 LinearLayout 添加一些视图(主要是 TextView),但它仅适用于某些设备。我已经在 Samsung Galaxy S5、Lenovo Tab2 和 Samsung 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>
解决方案
您需要将布局参数设置为您创建的 textview
textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
推荐阅读
- android - D8:程序类型已经存在:android.support.v4.media.MediaBrowserCompat$CustomActionCallback
- ibm-cloud-private - 列出 IBM ICP v3 中的集群
- json - 如何使用 OpenAPI Codegen 防止嵌套模型?
- javascript - 使用 Express/Node 编写的嵌套 Google DataStore 查询未获取预期数据
- javascript - 读取文本文件,通过 PHP 回显它并每隔几秒用 JS 刷新它
- hyperledger-fabric - 你可以在没有对等组织的情况下启动一个排序器吗?
- pandas - Pandas 中的概率记录链接
- python - 如何修复我的机器人在嵌入消息中找不到关键字?(discord.py)
- r - 在 plm 函数中从 Breusch Godfrey 测试中提取结果
- java - 需要可重用的通用方法,该方法使用杰克逊库从 JSONString 转换为对象列表