プログラムで textView を作成しています。この textView のスタイルを設定する方法はありますか? に似たもの
style="@android:style/TextAppearance.DeviceDefault.Small"
ファイルがある場合に使用しlayout.xml
ます。
プログラムで textView を作成しています。この textView のスタイルを設定する方法はありますか? に似たもの
style="@android:style/TextAppearance.DeviceDefault.Small"
ファイルがある場合に使用しlayout.xml
ます。
ビューのスタイルをプログラムで設定することはできませんが、textView.setTextAppearance(context, android.R.style.TextAppearance_Small);
.
現在、View のスタイルをプログラムで設定することはできません。
これを回避するには、スタイルが割り当てられたテンプレート レイアウト xml ファイルを作成します。たとえば、次の内容でres/layout
create as を実行します。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" />
次に、これを膨らませて、新しい TextView をインスタンス化します。
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
Compat バージョン (API 23 で廃止されたため):
TextViewCompat.setTextAppearance(textView, android.R.style.TextAppearance_DeviceDefault_Small);
これを試して
textview.setTextAppearance(context, R.style.yourstyle);
これはうまくいかないかもしれません このようなテキストビューで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" />
目的のスタイルを取得するには、textview を含む xml を膨張させます
TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvstyle, null);
実行時に (コードで) 生成されたビューの場合、スタイルをコンストラクターに渡すことができます: View(Context, AttributeSet, int)
それ以外の場合は、さまざまなメソッドを呼び出して外観を変更する必要があります