TextView在应该显示时不显示为粗体

时间:2014-03-03 17:47:08

标签: android textview

这是我的片段布局:

    

    <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的概述(蓝色突出显示的部分是包含的布局):

Overview in Android Studio

以下是Nexus 5的结果:

Result on a 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) {
    }
}

现在它按预期工作。

1 个答案:

答案 0 :(得分:1)

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

相关问题