来自assets文件夹的字体文件,设置为自定义布局的textview是不可见的android

时间:2014-01-09 15:46:43

标签: android

我尝试更改位于自定义布局中的textview的typeFace。当将text设置为textview时,textview变为不可见,但在原始布局文件中我可以看到typeFace已更改但未以编程方式反映....请帮我解决此问题

list_row.xml //自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#F0F0F0"
    android:orientation="horizontal"
    android:padding="5dip">

    <!-- ListRow Left sied Thumbnail image -->

    <LinearLayout
        android:id="@+id/list_row"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:background="@drawable/image_bg"
        android:padding="3dip" >

        <!-- THUMBAIL IMAGE -->

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="85dip"
            android:layout_height="80dip"
            android:scaleType="fitXY"
            android:shadowColor="#585858"
            android:shadowDx="0.0"
            android:shadowDy="0.0"
            android:shadowRadius="20"
            android:src="@drawable/karthik" />
    </LinearLayout>

    <TextView
        android:id="@+id/sub_title_category"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dip"
        android:gravity="right"
        android:text="Facebook Friend"
        android:textColor="#10bcc9"
        android:textSize="12dip"
        android:textStyle="bold" />
    <!-- ARROW IMAGE -->


    <!-- NAME -->
    <!-- CONTACT NUMBER -->


    <!-- BIRTHDATE -->

    <TextView
        android:id="@+id/title_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/list_row"
        android:layout_toRightOf="@+id/list_row"
        android:text="KARTHIK.KOLANJI"
        android:textColor="#181818"
        android:textSize="16dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
        android:id="@+id/txtview_type_of_reminder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/list_row"
        android:layout_below="@+id/list_row"
        android:layout_marginTop="3dip"
        android:text="(Anniversary)"
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/txtview_zodiac"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView02"
        android:layout_alignBottom="@+id/TextView02"
        android:layout_toRightOf="@+id/TextView02"
        android:text="Sagittarius"
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" />

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_above="@+id/txtview_type_of_reminder"
        android:layout_toRightOf="@+id/txtview_zodiac"
        android:scaleType="fitXY"
        android:shadowColor="#8B4513"
        android:shadowDx="0.0"
        android:shadowDy="0.0"
        android:shadowRadius="20"

        android:layout_marginLeft="5dip"
        android:contentDescription="Zodiac icon"/>

    <TextView
        android:id="@+id/txtview_turns"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/list_image_arrow"
        android:layout_below="@+id/list_image_arrow"
        android:text="24"
        android:textColor="#181818"
        android:textSize="20sp"
         />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtview_type_of_reminder"
        android:layout_alignBottom="@+id/txtview_type_of_reminder"
        android:layout_alignLeft="@+id/TextView02"
        android:text="Next birthday on : "
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" 
        android:layout_marginTop="3dip"/>

    <TextView
        android:id="@+id/txtview_next_bd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView01"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_toRightOf="@+id/TextView01"
        android:text="Wednesday"
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" 
        android:layout_marginTop="2dip"/>

    <TextView
        android:id="@+id/sub_title_contact_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_name"
        android:layout_toRightOf="@+id/list_row"
        android:text="9594080469"
        android:textColor="#181818"
        android:textSize="15dip" />

    <TextView
        android:id="@+id/sub_title_birthdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/sub_title_contact_number"
        android:layout_below="@+id/sub_title_contact_number"
        android:text="7th, April 1990"
        android:textColor="#181818"
        android:textSize="15dip"
        android:layout_marginTop="3dip" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/ImageView01"
        android:layout_toRightOf="@+id/list_row"
        android:text="Zodiac :  "
        android:textColor="#181818"
        android:textSize="15sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/list_image_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sub_title_birthdate"
        android:layout_alignRight="@+id/sub_title_category"
        android:text="t u r n s"
        android:textColor="#181818"
        android:textSize="12sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/ImageView01"
        android:text=" days left"
        android:textColor="#181818"
        android:textSize="12sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/txtview_days_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/txtview_next_bd"
        android:layout_alignBottom="@+id/txtview_next_bd"
        android:layout_alignLeft="@+id/txtview_turns"
        android:layout_alignParentRight="true"
        android:text="365"
        android:textColor="#181818"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

SearchListActivity.java //引用自定义布局文件

public class SearchListActivity extends ListActivity {

    String from[];
    int to[];
    CustomCursorAdapter adapter;

    TextView emptyView;

    @SuppressLint("NewApi") @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_list_activity);
        Cursor cursor;
        adapter=new CustomCursorAdapter(this, cursor);
        cursor=cursorLoader.loadInBackground();
        from=new String[]{BirthdayProvider.NAME,BirthdayProvider.NUMBER,BirthdayProvider.CATEGORY,BirthdayProvider.EVENT_TYPE,BirthdayProvider.EVENT_DAY,BirthdayProvider.TURNS_AGE,BirthdayProvider.EVENT_WEEK,BirthdayProvider.WESTERN_ZODIAC,BirthdayProvider.DAYS_REMAINING};
        to=new int[]{R.id.title_name,R.id.sub_title_contact_number,R.id.sub_title_category,R.id.txtview_type_of_reminder,R.id.sub_title_birthdate,R.id.txtview_turns,R.id.txtview_next_bd,R.id.txtview_zodiac,R.id.txtview_days_left};
        if (cursor.moveToFirst()) {

            adapter=new CustomSimpleCursor(this, R.layout.list_row, cursor, from, to);
            getListView().setAdapter(adapter);
        }
        else
        {
            emptyView=(TextView)getListView().findViewById(android.R.id.empty);
        }

        setFont();
    }

    public void setFont(){
        LayoutInflater inflater=LayoutInflater.from(this);
        View view =inflater.inflate(R.layout.list_row, null);

        Typeface font;
        TextView txtDaysRemainig=(TextView)view.findViewById(R.id.txtview_days_left);
        font=Typeface.createFromAsset(getAssets(), "CROCHET PATTERN.ttf");
        txtDaysRemainig.setTypeface(font);
    }
}

CustomCursorAdapter .java

public class CustomCursorAdapter extends CursorAdapter{

    Typeface font;
    TextView txtDaysRemainig,txtTurnsAge;

    public CustomCursorAdapter(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View arg0, Context arg1, Cursor cursor) {
        // TODO Auto-generated method stub
        String year=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_YEAR));
        String month=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_MONTH));
        String date=cursor.getString(cursor.getColumnIndex(BirthdayProvider.EVENT_DATE));
        String remainingDays=BirthdayCalculation.getDaysRemainingForNextBirthDay(year, month, date);
        txtDaysRemainig.setText(remainingDays);
    }

    @Override
    public View newView(Context context, Cursor arg1, ViewGroup parent) {

        LayoutInflater mInflater=LayoutInflater.from(context);
        View view = mInflater.inflate(R.layout.list_row, parent, false);

        txtDaysRemainig = (TextView)view.findViewById(R.id.txtview_days_left);
        font=Typeface.createFromAsset(context.getAssets(), "CrochetPattern.ttf");
        txtDaysRemainig.setTypeface(font);
        // edit: no need to call bindView here. That's done automatically
        return view;

    }

}

2 个答案:

答案 0 :(得分:0)

检查您的ttf文件是否位于assets目录中。

然后我建议你将ttf“CROCHET PATTERN.ttf”重命名为“CrochetPattern.ttf”,也许这里的“空间”是个麻烦。

编辑: 我没有意识到你在ListView。 您需要在适配器的TypeFace方法中设置newView。所以在这里你需要覆盖CursorAdapter。就像在这段代码中一样:

public class CustomCursorAdapter extends CursorAdapter {
    private Context context;
    LayoutInflater mInflater;

    public AreaCursorAdapter(Context context, Cursor c) {
        // that constructor should be used with loaders.
        super(context, c, 0);
        mInflater = LayoutInflater.from(context);
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        // put the data on your views           

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mInflater.inflate(R.layout.list_row, parent, false);
        Typeface font;
        TextView txtDaysRemainig = (TextView)view.findViewById(R.id.txtview_days_left);
        font=Typeface.createFromAsset(getAssets(), "CrochetPattern.ttf");
        txtDaysRemainig.setTypeface(font);
        // edit: no need to call bindView here. That's done automatically
        return v;
    }

}

因为在您从未在任何地方附加的布局修改TextView之前。

并替换:

adapter=new CustomSimpleCursor(this, R.layout.list_row, cursor, from, to);

通过

adapter = new CustomCursorAdapter(this, cursor);

答案 1 :(得分:0)

在设置字体

后尝试手动设置txtDatsRemaining.setText("365");文本