繁体   English   中英

如何从LinearLayout自己的样式设置LinearLayout的子视图样式?

[英]How to style the child view of a LinearLayout from the LinearLayout's own style?

抱歉,标题可能令人困惑

因此,我使用的是ViewPagerIndicator,这是在5.0版TabLayout发行之前通常用于标签页的库。 在此库中,选项卡是扩展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设置样式。 我怎样才能做到这一点?

当然,我也可以在TabViewWithIcon内以编程方式为TextView传递样式,例如

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

但是我不得不在库中编写自定义样式,这是我不应该做的。

我需要重新定义一些属性或某些东西吗? 我处理方法有误吗?

我是个白痴,我只是将自定义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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM