2013-04-01 78 views
0

我以編程方式創建了一個textView。現在我需要以編程方式爲此textview分配樣式。 (不僅TextAppearance,但也留下邊距,填充等)。所有這些樣式屬性都是在xml文件中定義的。如何以編程方式將樣式應用於Textview

TextView tv = new TextView(this); 

回答

0

設置填充非常簡單。但利潤有點不穩定。請看下面。

TextView tv = new TextView(this); 
//setting padding left top right bottom 
tv.setPadding(10, 10, 10, 10); 
//setting left margin 
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom); 
tv.setLayoutParams(llp); 
0
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    tv.setLayoutParams(params); 
    tv.setPadding(left, top, right, bottom); 
    left- padding to left 
    top - padding to right 
    right -padding to right 
    bottom -padding to bottom 

更多造型檢查下面的鏈接。請參閱setter方法。您可以設置文字大小,背景顏色等等。

http://developer.android.com/reference/android/widget/TextView.html

0

使用佈局參數設置此屬性。

TextView t = new TextView(this); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(heightInPixels, widthInPixel); 
params.setMargins(left, top, right, bot); 
t.setLayoutParams(params); 
mLinearLayout.addView(t); 

編輯:

如果你有自己的風格,能夠在attrs.xml文件中定義的,那麼你必須使自己的TextView和構造手柄屬性你想要的。它用於處理來自XML的屬性。而對於java定義,你必須制定「設置方法」。

public class MyTextView extends TextView 
{ 
    String _stringValue; 

    public MyTextView(Context c) 
    { 
     this(c, null); 
    } 

    public MyTextView(Context c, AttributeSet attrs) 
    { 
     this(c, attrs, 0); 
    } 

    public MyTextView(Context c, AttributeSet attrs, int defStyle) 
    { 
      super(c, attrs, defStyle); 
     TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.MyStyleAble); 
     _stringValue = a.getString(R.styleable.MyStyleAble_stringValue); 
     a.recycle(); //don't forget this line! 
    } 

    public void setStringValue(String s) 
    { 
     _stringValue = s; 
    } 
} 

編輯

在這裏,在代碼中使用MyTextView的。只需使用標準構造函數創建它即可。

​​

然後設置你的風格能力值。在我的例子中它是字符串值。

t.setStringValue("My string text"); 

然後使用layoutParams。

t.setLayoutParams(params); 

並將新創建的TextView添加到某些佈局。例如LinearLayout。

mLinearLayout.addView(t); 

我忘了說,你必須定義樣式能夠INT attrs.xml只有當你需要在XML文件中設置你的屬性。 然後你就這樣使用它。

<my.package.name.MyTextView 
    xmlns:custom="http://schemas.android.com/apk/res/my.package.name" 
    custom:stringValue="some string text" 
    //and also you can use standart android:... attributes 
/> 
+0

感謝您的回覆左邊距和填充, 但我的問題是,我有在attr中定義的樣式。XML <聲明-設置樣式名稱= 「ClassicWidget」> 它可能包括邊距,填充和其他樣式項目 我必須將這些樣式應用於文本視圖 – user1746619

+0

@ user1746619好的。你能舉一些例子嗎?我不確定我是否理解你想做什麼。 – WELLCZECH

+0

@ user1746619答覆已更新。我希望這是你想要做的,這對你有幫助。 – WELLCZECH

0

使用layoutParams設置動態創建的textView的邊距。

TextView view1 = new TextView(this); 

view1.setGravity(Gravity.RIGHT | Gravity.CENTER); 

RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(

         RelativeLayout.LayoutParams.WRAP_CONTENT, 

         RelativeLayout.LayoutParams.WRAP_CONTENT); 

rlp.setMargins(10,10, 0, 0); 
view1.setLayoutParams(rlp); 
0

的字體樣式:textview.setTypeface(Typeface.DEFAULT_BOLD);

你需要定義 「佈局PARAMS」 像

TextView tv = new TextView(this); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(heightInPixels, widthInPixel); 
tv.setLayoutParams(params); 
tv.setMargins(10, 10, 10, 10); 
yourLayout.addView(tv); 
tv.setPadding(10, 10, 10, 10); 
相關問題