[英]How to style the child view of a LinearLayout from the LinearLayout's own style?
Sorry for the possibly confusing title 抱歉,标题可能令人困惑
So I'm using ViewPagerIndicator, which is a library commonly used for tabs before TabLayout was released in 5.0. 因此,我使用的是ViewPagerIndicator,这是在5.0版TabLayout发行之前通常用于标签页的库。 In this library, tabs are views that extend TextView, that accepted a custom attribute for styling. 在此库中,选项卡是扩展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 vpi__attrs.xml
<resources>
<declare-styleable name="ViewPagerIndicator">
...
<!-- Style of the tab indicator's tabs. -->
<attr name="vpiTabPageIndicatorStyle" format="reference"/>
</declare-styleable>
...
With this setup, when I used TabPageLayout in my own project, I could define the style of the text like this 通过此设置,当我在自己的项目中使用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>
The following style would be applied to the Activity, and it would override the default vpiTabPageIndicator in the ViewPagerIndicator library. 以下样式将应用于Activity,它将覆盖ViewPagerIndicator库中的默认vpiTabPageIndicator。
My problem now is that I needed to make more customization to TabView than a TextView would allow, so I created a new inner class called "TabLayoutWithIcon" that extends LinearLayout and includes a TextView. 我现在的问题是,我需要对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
}
Now the same custom style is being applied to a LinearLayout, but I also want to style the child TextView. 现在,将相同的自定义样式应用于LinearLayout,但是我还想为子TextView设置样式。 How can I do this? 我怎样才能做到这一点?
Of course, I could also just pass in a style for the TextView programatically inside TabViewWithIcon,like 当然,我也可以在TabViewWithIcon内以编程方式为TextView传递样式,例如
mText.setTextAppearance(context, R.style.CustomTabTextStyle);
but then I would have to write my custom style inside the library, which I shouldn't be doing. 但是我不得不在库中编写自定义样式,这是我不应该做的。
Do I need to redefine some attributes or something? 我需要重新定义一些属性或某些东西吗? Am I approaching this incorrectly? 我处理方法有误吗?
Im an idiot, I just have pass the custom TextView style into the TextView 我是个白痴,我只是将自定义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.