【问题标题】:TextView don't appear as bold while they shouldTextView 不应该显示为粗体
【发布时间】:2014-03-04 02:15:43
【问题描述】:

这是我的片段布局:

    <RelativeLayout
        android:id="@+id/issue_main_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/issue_is_favorite"
            android:button="@drawable/favorite"
            android:padding="4dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignBottom="@+id/issue_tracker_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <net.bicou.redmine.widget.RobotoThinTextView
            android:id="@+id/issue_tracker_id"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/issue_is_private"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="32sp"/>

        <TextView
            android:id="@+id/issue_is_private"
            android:layout_toLeftOf="@+id/issue_is_favorite"
            android:layout_alignBottom="@+id/issue_tracker_id"
            android:layout_marginBottom="6dp"
            android:text="@string/issue_is_private"
            android:padding="4dp"
            android:gravity="center"
            android:visibility="gone"
            android:background="@drawable/issue_listitem_project_background_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

        <net.bicou.redmine.widget.RobotoCondensedLightTextView
            android:id="@+id/issue_subject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_below="@+id/issue_is_favorite"
            android:layout_alignParentLeft="true"/>

        <ImageView
            android:id="@+id/issue_author_avatar"
            android:src="@drawable/no_avatar"
            android:layout_margin="8dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/issue_parent"
            android:layout_width="42dp"
            android:layout_height="42dp"/>

        <TextView
            android:id="@+id/issue_author"
            android:gravity="center_vertical"
            android:textColor="@color/text_light"
            android:layout_alignTop="@+id/issue_author_avatar"
            android:layout_toRightOf="@+id/issue_author_avatar"
            android:padding="4dp"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:layout_alignBottom="@+id/issue_author_avatar"/>

        <!--TODO the height of author shouldn't be hardcoded but if it's not the text is not centered vertically-->

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/issue_subject"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

        <include layout="@layout/inc_issue_overview_fields"/>

    </RelativeLayout>

</ScrollView>

在最底部有一个包含的布局。

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_status"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_status"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_priority"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_priority"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_assignee"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_assignee"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:text="@string/issue_category"/>

        <net.bicou.redmine.widget.RobotoTextView
            android:id="@+id/issue_category"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_target_version"/>

        <TextView
            android:id="@+id/issue_target_version"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_start_date"/>

        <TextView
            android:id="@+id/issue_start_date"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_due_date"/>

        <TextView
            android:id="@+id/issue_due_date"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_percent_done"/>

        <TextView
            android:id="@+id/issue_percent_done"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_spent_time"/>

        <TextView
            android:id="@+id/issue_spent_time"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <TextView
            style="@style/IssueLabel"
            android:text="@string/issue_estimated_hours"/>

        <TextView
            android:id="@+id/issue_estimated_hours"
            style="@style/IssueValue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <View
            android:layout_weight="1"
            android:layout_span="2"
            android:layout_width="match_parent"
            android:layout_height="12dp"/>

    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <net.bicou.redmine.widget.RobotoTextView
            style="@style/IssueLabel"
            android:layout_weight="1"
            android:text="@string/issue_description"
            android:layout_span="2"/>

    </TableRow>

    <TableRow
        style="@style/IssueTableRow"
        android:layout_marginTop="4dp">

        <View
            android:background="#CC0000"
            android:layout_weight="1"
            android:layout_span="2"
            android:layout_width="match_parent"
            android:layout_height="2dp"/>
    </TableRow>

    <TableRow style="@style/IssueTableRow">

        <WebView
            android:id="@+id/issue_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_span="2"/>
    </TableRow>

</TableLayout>

还有一个 -sw720dp 和一个 -sw600dp-land 用于 10" 和 7" 横向设备。

以下是应用于该片段/活动的样式的相关部分:

<style name="IssueLabel">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:layout_weight">1</item>
</style>

<style name="IssueValue">
    <item name="android:layout_weight">2</item>
</style>

同样,它是默认(手机)版本,还有另一个用于 10" 和 7" 横向设备的版本。

这定义了一个基本的TextView,其中的文本应该是粗体。
这是一个自定义视图,让我们看看它是如何定义的:

public class RobotoTextView extends CustomTypefaceTextView {
    public RobotoTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void createFont() {
        super.createFont("Roboto-Regular.ttf");
    }
}

(in another file:)

public abstract class CustomTypefaceTextView extends TextView {
    private final Context mContext;

    public CustomTypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        createFont();
    }

    public CustomTypefaceTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        createFont();
    }

    public CustomTypefaceTextView(final Context context) {
        super(context);
        mContext = context;
        createFont();
    }

    /**
     * This one must call super.createFont(String)
     */
    protected abstract void createFont();

    public void createFont(String typeface) {
        if (isInEditMode()) {
            return;
        }
        try {
            final Typeface font = Typeface.createFromAsset(mContext.getAssets(), typeface);
            setTypeface(font);
        } catch (final Exception e) {
        }
    }
}

这是来自 Android Studio 的概述(蓝色突出显示的部分是包含的布局):

这是 Nexus 5 上的结果:

如您所见(以及屏幕上的问题所示 :)),前四个字段不是粗体。我没有更改代码中的任何内容,所以我不明白发生了什么。

有什么线索吗?


感谢 Timothy Miko,我已将我的 CustomTypeFaceTextView#createFont(String) 方法修复如下:

public void createFont(String typeface) {
    if (isInEditMode()) {
        return;
    }
    try {
        final Typeface font = Typeface.createFromAsset(mContext.getAssets(), typeface);
        Typeface currentFont = getTypeface();
        if (currentFont != null) {
            setTypeface(font, currentFont.getStyle());
        } else {
            setTypeface(font);
        }
    } catch (final Exception e) {
    }
}

现在它可以按预期工作了。

【问题讨论】:

  • 高亮显示错误的文本视图是一件好事
  • 你是什么意思,@SrinathGanesh?

标签: android textview


【解决方案1】:

问题在于net.bicou.redmine.widget.RobotoTextView。您可以通过编程方式创建 Typface,然后在 `TextView 上调用 setTypeface(),而不是定义自己的自定义 TextView。

【讨论】:

  • 或者,您的自定义 TextView 中没有 setTypface()...所以永远不会检索到 android:textStyle 属性。见:stackoverflow.com/questions/8954484/…
  • 所有标签都使用相同的 XML 代码,因此使用相同的 RobotoTextView;但它们的显示方式不同。
  • 在 RobotoTextView 的 line 46 中调用 setTypeface(font)。这是在使用 xml 属性的 super() 中调用第一个 setTypeface(Typeface tf, int style) 之后调用的。所以在 RobotoTextView 中,如果你将 line 46 更改为 setTypface(font, Typeface.BOLD) 那么它应该可以工作。
  • 哇,不错的收获。我已编辑此行以添加getTypeface() 并将.getStyle() 传递给setTypeFace()(如果它不为空)。我在问题的底部添加了这个。非常感谢!
  • 另外,你的答案缺少反引号,这让我很烦:)
猜你喜欢
  • 1970-01-01
  • 2013-08-06
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
相关资源
最近更新 更多