lok*_*oko 31 android styles textview android-styles
我正在以编程方式创建textView.有没有办法可以设置这个textView的样式?类似的东西
style="@android:style/TextAppearance.DeviceDefault.Small"
Run Code Online (Sandbox Code Playgroud)
如果我有一个layout.xml文件,我将使用它.
Mat*_*att 53
您无法以编程方式设置View的样式,但您可能能够执行类似的操作textView.setTextAppearance(context, android.R.style.TextAppearance_Small);.
Rag*_*ood 29
目前无法以编程方式设置View的样式.
要解决此问题,您可以创建一个分配了样式的模板布局xml文件,例如在res/layoutcreate tvtemplate.xml中使用以下内容:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="@android:style/TextAppearance.DeviceDefault.Small" />
Run Code Online (Sandbox Code Playgroud)
然后膨胀这个实例化你的新TextView:
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
Run Code Online (Sandbox Code Playgroud)
小智 10
实际上,从API级别21开始,这是可能的.
TextView有一个4参数构造函数:
TextView (Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,不需要中间的两个参数.以下代码直接在活动中创建TextView,并仅定义其样式资源:
TextView myStyledTextView = new TextView(this, null, 0, R.style.my_style);
Run Code Online (Sandbox Code Playgroud)
尝试这个
textview.setTextAppearance(context, R.style.yourstyle);
Run Code Online (Sandbox Code Playgroud)
这可能无法正常工作,尝试像这样用textview创建xml
textviewstyle.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.DeviceDefault.Small" />
Run Code Online (Sandbox Code Playgroud)
要获得所需的样式,请膨胀包含textview的xml
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvstyle, null);
Run Code Online (Sandbox Code Playgroud)
兼容版本(由于api 23中已弃用):
TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33478 次 |
| 最近记录: |