我在ViewPager
中使用PagerTabStrip
来显示每个页面的标题。下面是我的XML代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_title_strip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#a4c639"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
我想在PagerTabStrip
中将textStyle设置为粗体,如下所示:android:textStyle="bold"
。但这在PagerTabStrip
中是不可能的。看起来它只有文本的textcolor
属性。将样式设置为粗体的方法是什么?
发布于 2012-11-17 05:46:52
您需要做的是为文本创建一个样式,然后使用android:text外观属性...
可能是这样的:
<style name="PagerTabStripText">
<item name="android:textSize">14sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#a4c639</item>
</style>
然后在您的PagerTabStrip XML中执行以下操作:
<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_title_strip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:textAppearance="@style/PagerTabStripText"/>
发布于 2014-09-11 21:57:15
PagerTabStrip不接收textAppearance。
就像@Justin上面说的,但是在xml put上:
style="@style/PagerTabStripText"
不是:
android:textAppearance="@style/PagerTabStripText"
发布于 2014-10-09 02:04:11
如果你想让中间的标签标题不同于其他标签,那么你需要使用反射:
try {
final Class pagerTitleStripClass = PagerTitleStrip.class;
final Field mCurrTextField = pagerTitleStripClass.getDeclaredField("mCurrText");
mCurrTextField.setAccessible(true);
// mTitle is my class's PagerTabStrip member variable
final TextView mCurrText = (TextView) mCurrTextField.get(mTitle);
mCurrText.setTypeface(Typeface.DEFAULT_BOLD);
} catch (final Exception e) {
Log.w(TAG, "Exception when styling currently selected title!", e);
}
不幸的是,当Android Support Library的更新发布时,这个解决方案可能不再起作用(因为PagerTitleStrip类就是从那里来的)。具体地说,此代码示例使用revision 20。
https://stackoverflow.com/questions/13307758
复制相似问题