2016-02-16 42 views
1

改變textappearance動態添加的行的介質上點擊按鈕BTN1一排是added.I希望其文本外觀改變爲MEDIUM於表佈局分鐘API 17

ERROR-setTextAppearance不能應用於

btn1.setOnClickListener(new View.OnClickListener() { 
              @Override 
              public void onClick(View v) { 


                TableRow row1 = new TableRow(create_form.this); 
                TextView txt1 = new TextView(create_form.this); 
                EditText edttxt1 = new EditText(create_form.this); 

                txt1.setText("Topic"); 
                txt1.setTextColor(Color.parseColor("#000000")); 
                txt1.setTextAppearance(android.R.style.TextAppearance_Medium);**//ERROR ,NOT WORKING**,Call requires API level23(current min is 17) 





                row1.addView(txt1); 
                row1.addView(edttxt1); 

                t1.addView(row1); 

               } 

              } 


       }); 

回答

0

低於API級別23使用這樣的:

txt1.setTextAppearance(this, android.R.style.TextAppearance_Medium); 

由於API 23,你不需要通過上下文參數。因此,你可以簡單地調用:

txt1.setTextAppearance(android.R.style.TextAppearance_Medium); 
+0

這是說錯了第一個argumnet類型爲txt1.setTextAppearance(this,android.R.style.TextAppearance_Medium); –

+0

txt1.setTextAppearance(android.R.style.TextAppearance_Medium);關於使用此應用程序崩潰的按鈕,請點擊 –

+0

您遇到應用程序崩潰有哪些例外情況? –

0

另外,除了羅希特夏爾馬的迴應,你可以定義custome風格爲您textViews並將它們設置程序,這樣`假如你的風格是這樣的:

<style name="MyStyle" parent="@android:style/Widget.TextView"> 
    <item name="android:textStyle">bold</item> 
    <item name="android:textAppearance">?android:textAppearanceSmall</item> 
    <item name="android:typeface">sans</item> 
    <item name="android:textColor">@android:color/white</item> 
    ... 
</style> 

TextView textViewTitle = (TextView) findViewById(R.id.text_view_title); // Your TextView 
textViewTitle.setTextAppearance(this, R.style.MyStyle); 

好處是,您可以設置其他重要的屬性,如textSize,字體,textColor,只有一次調用。 setTextAppearance方法

+0

嘿一秒。我認爲它的工作。但它沒有...方法textAppearance是棄用。 –

+0

很高興我能幫到你。請讓答案接受並upvote,如果它有助於解決您的問題@SomyaArora –

+0

嘿一秒。我認爲它的工作。但它沒有...方法textAppearance已棄用。 –

0

找到替代解決方案,IT-

代替改變textappearance變化字體大小 txt1.setTextSize(TypedValue.COMPLEX_UNIT_SP,18)的;

18sp =中等文字外觀。