android - 从模板 XML 文件创建 TextView

标签 android layout textview

我在文件 simple_txt.xml

中有一个简单的 TextView 模板
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:textSize="18sp" >
</TextView>

在一些代码中,我需要从这个模板创建一个 TextView,类似于:

TextView txt = new TextView(this);
txt.setLayout(R.layout.simple_txt);//???

然后用它做点什么(setText 等)。如何创建这样的 TextView

最佳答案

TextView txt = (TextView) View.inflate(this, R.layout.simple_txt, null);

关于android - 从模板 XML 文件创建 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10129953/

相关文章:

ios - Xcode 约束

java - 布局管理器 preferredSize Java

android - 在自定义 DialogFragment 中初始化 TextView

android - 使用 SharedPreferences 显示字符串不起作用

android - 将 android TextView 更改为 ListView

android - 允许 TextView 在屏幕之外

android - AlertDialog 不会显示在 onCreate 方法中

android - 如何从 Drawable 获取 URI

Android proguard 不完整的堆栈跟踪

android - AlertDialog EditText 不显示软键盘