假设我有以下.xml
风格:
<style name="MaterialPreviewDetailsTextSmall">
<item name="android:layout_marginLeft">@dimen/material_margin</item>
<item name="android:textColor">@color/material_preview_backgroud</item>
<item name="android:textSize">@dimen/material_normal_smal_text</item>
<item name="android:textStyle">bold</item>
</style>
我想通过属性将它传递给自定义视图:
<pl.valueadd.ledoc.view.ViewPreviewRow
...
app:view_title="@string/equipment_overview_id_label"
app:view_title_style="@style/MaterialPreviewDetailsTextSmall"/>
在自定义视图中,我获取样式属性并将其应用于TextView
:
public class ViewPreviewRow extends RelativeLayout {
TextView tvTitle;
public ViewPreviewRow(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
initView(context);
obtainStyledAttributes(context, attrs);
}
private void obtainStyledAttributes(Context context, AttributeSet attrs) {
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ViewPreviewRow);
CharSequence text = attributes.getString(R.styleable.ViewPreviewRow_view_title);
if (text != null) {
tvTitle.setText(text);
} else {
throw new NullPointerException("Attribute view_title cannot be null");
}
int appearance = attributes.getResourceId(R.styleable.ViewPreviewRow_view_title_style, 0);
if (appearance != 0) {
setTextViewAppearance(tvTitle, appearance);
tvTitle.requestLayout();
}
...
attributes.recycle();
}
问题是:
为什么文本有合适的颜色、大小、marginLeft 但不是粗体?