2016-03-01 63 views
1

对不起,可能会令人困惑的标题如何从LinearLayout自己的样式设置LinearLayout的子视图?

所以我使用ViewPagerIndicator,这是一个在TabLayout 5.0版本发布之前常用的标签库。在这个库中,选项卡是扩展TextView的视图,它接受样式的自定义属性。

//An inner class of TabPageLayout 
private class TabView extends TextView { 
    private int mIndex; 

    public TabView(Context context) { 
     super(context, null, R.attr.vpiTabPageIndicatorStyle); //<--custom attribute 
    } 
    // ... 
} 

vpi__attrs.xml

<resources> 
    <declare-styleable name="ViewPagerIndicator"> 
     ... 
     <!-- Style of the tab indicator's tabs. --> 
     <attr name="vpiTabPageIndicatorStyle" format="reference"/> 
    </declare-styleable> 
    ... 

采用这种设置,当我在自己的项目中使用TabPageLayout,我可以定义文本的样式像这样

<!--This is styles.xml of my project --> 
<style name="MyStyle.Tabs" parent="MyStyle" > 
     <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item> 

    </style> 

    <style name="CustomTabPageIndicator"> 
     <item name="android:gravity">center</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textSize">@dimen/secondary_text_size</item> 
     ... 
    </style> 

以下样式将应用于Activity,并且它将覆盖ViewPagerIndicator库中的默认vpiTabPageIndicator。

我现在的问题是,我需要对TabView进行更多的定制,而不是TextView允许的,所以我创建了一个名为“TabLayoutWithIcon”的新内部类,它扩展了LinearLayout并包含了一个TextView。

private class TabViewWithIcon extends LinearLayout { 
    private int mIndex; 
    private TextView mText; 

    public TabViewWithIcon(Context context) { 
     super(context, null, R.attr.vpiTabPageIndicatorStyle); 
     //setBackgroundResource(R.drawable.vpi__tab_indicator); 
     mText = new TextView(context); 
    } 
    ... 

    public void setText(CharSequence text){ 
     mText.setText(Integer.toString(mIndex) + " tab"); 
     addView(mText); 
    } 

    public void setImage(int iconResId){ 
     mText.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0); 
     mText.setCompoundDrawablePadding(8); //Just temporary 
    } 

现在将相同的自定义样式应用于LinearLayout,但我也想对样式TextView子样式。我怎样才能做到这一点?

当然,我也可以只通过在TextView的编程内TabViewWithIcon一个风格,像

 mText.setTextAppearance(context, R.style.CustomTabTextStyle); 

但我会写的图书馆,我不应该在我的自定义样式这样做。

我是否需要重新定义某些属性或其他内容?我是否错误地处理了这个问题?

回答

0

林白痴,我只是将自定义的TextView风格融入TextView的

public TabView(Context context) { 
     super(context, null, R.attr.vpiTabPageIndicatorStyle); 
     //setBackgroundResource(R.drawable.vpi__tab_indicator); 
     mText = new TextView(context, null, R.attr.vpiTabPageIndicatorStyle); 
    } 
相关问题