我以编程方式创建了一个textView。现在我需要以编程方式为此textview分配样式。 (不仅TextAppearance,但也留下边距,填充等)。所有这些样式属性都是在xml文件中定义的。如何以编程方式将样式应用于Textview
TextView tv = new TextView(this);
我以编程方式创建了一个textView。现在我需要以编程方式为此textview分配样式。 (不仅TextAppearance,但也留下边距,填充等)。所有这些样式属性都是在xml文件中定义的。如何以编程方式将样式应用于Textview
TextView tv = new TextView(this);
设置填充非常简单。但利润有点不稳定。请看下面。
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);
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。
使用布局参数设置此属性。
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
/>
使用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);
的字体样式: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);
感谢您的回复左边距和填充, 但我的问题是,我有在attr中定义的样式。XML <声明-设置样式名称= “ClassicWidget”> declare-styleable> 它可能包括边距,填充和其他样式项目 我必须将这些样式应用于文本视图 –
user1746619
@ user1746619好的。你能举一些例子吗?我不确定我是否理解你想做什么。 – WELLCZECH
@ user1746619答复已更新。我希望这是你想要做的,这对你有帮助。 – WELLCZECH