<TextView
android:id="@+id/online_message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Onliners:"
/>
我想通过以下方式创建一个与上面相同的 TextView
:
android.widget.TextView.TextView(Context context, AttributeSet attrs)
但是我如何指定 attrs
以使其代表与上述相同的属性?
This answer很接近,但是有没有办法只读取属性(xml 中没有 TextView
标记)并解析为 AttributeSet
?
更新
我正在寻找一种以编程方式构建 AttributeSet
的方法。
最佳答案
我认为 AttributeSet 与 xml 布局结合使用效果更好。否则,您可以创建上述所需的 TextView,如下所示:
TextView t = new TextView(context);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
t.setTextAppearance(context, android.R.attr.textAppearanceLarge);
t.setText("Onliners:");
关于 Id,您实际上不需要在代码中设置它,因为您已经引用了整个对象。
关于java - 如何动态创建具有指定属性的 TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10280313/