2014-10-27 149 views
1

我建立了我自己的字典應用程序。但我嘗試在我的應用中使用自定義字體。然後它不能顯示單詞的定義。請幫助我在哪裏我是假的。我不知道如何使用自定義字體。我在哪裏假?如何在可搜索字典中使用自定義字體?

main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
/* 
** Copyright 2010, The Android Open Source Project 
** 
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
** 
**  http://www.apache.org/licenses/LICENSE-2.0 
** 
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License. 
*/ 
--> 
<!-- Layout for SearchableActivity. 
--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<TextView 
     android:id="@+id/text" 
     android:textColor="?android:textColorPrimary" 
     android:textSize="17dp" 
     android:text="@string/search_instructions" 
     android:background="@android:drawable/title_bar" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

<ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:cacheColorHint="@color/abc_search_url_text_pressed" /> 

</LinearLayout> 

MainActivity.java

package com.example.dictionary; 

import android.support.v4.widget.CursorAdapter; 
import android.support.v7.app.ActionBarActivity; 
import android.app.Activity; 
import android.app.ActionBar; 
import android.app.SearchManager; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Typeface; 
import android.net.Uri; 
import android.os.Build; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.SearchView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 
import android.widget.AdapterView.OnItemClickListener; 

/** 
* The main activity for the dictionary. 
* Displays search results triggered by the search dialog and handles 
* actions from search suggestions. 
*/ 
public class MainActivity extends Activity { 

private TextView mTextView; 
private ListView mListView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mTextView = (TextView) findViewById(R.id.text); 
    mListView = (ListView) findViewById(R.id.list); 

    handleIntent(getIntent()); 
} 

@Override 
protected void onNewIntent(Intent intent) { 
    // Because this activity has set launchMode="singleTop", the system calls this method 
    // to deliver the intent if this activity is currently the foreground activity when 
    // invoked again (when the user executes a search from this activity, we don't create 
    // a new instance of this activity, so the system delivers the search intent here) 
    handleIntent(intent); 
} 

private void handleIntent(Intent intent) { 
    if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
     // handles a click on a search suggestion; launches activity to show word 
     Intent wordIntent = new Intent(this, WordActivity.class); 
     wordIntent.setData(intent.getData()); 
     startActivity(wordIntent); 
    } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     // handles a search query 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     showResults(query); 
    } 
} 

/** 
* Searches the dictionary and displays results for the given query. 
* @param query The search query 
*/ 
private void showResults(String query) { 

    Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null, 
          new String[] {query}, null); 

    if (cursor == null) { 
     // There are no results 
     mTextView.setText(getString(R.string.no_results, new Object[] {query})); 
    } else { 
     // Display the number of results 
     int count = cursor.getCount(); 
     String countString = getResources().getQuantityString(R.plurals.search_results, 
           count, new Object[] {count, query}); 
     mTextView.setText(countString); 

     // Specify the columns we want to display in the result 
     String[] from = new String[] { DictionaryDatabase.KEY_WORD, 
             DictionaryDatabase.KEY_DEFINITION }; 

     // Specify the corresponding layout elements where we want the columns to go 
     int[] to = new int[] { R.id.word, 
           R.id.definition }; 

     // Create a simple cursor adapter for the definitions and apply them to the ListView 
     SimpleCursorAdapter words = new SimpleCursorAdapter(this, 
             R.layout.result, cursor, from, to); 
     mListView.setAdapter(words); 

     // Define the on-click listener for the list items 
     mListView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // Build the Intent used to open WordActivity with a specific word Uri 
       Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 
       Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI, 
               String.valueOf(id)); 
       wordIntent.setData(data); 
       startActivity(wordIntent); 
      } 
     }); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.options_menu, menu); 

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){ 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     searchView.setIconifiedByDefault(false); 
    } 

    return true; 
} 


public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.search: 
      onSearchRequested(); 
      return true; 
     default: 
      return false; 
    } 
} 
public class CustomFontAdapter extends SimpleCursorAdapter { 
    public CustomFontAdapter(final Context context, final int layout, final Cursor c, final String[] from, 
       final int[] to, final int flags) { 
      super(context, layout, c, from, to, flags); 
    } 

    @Override 
    public void bindView(final View view, final Context context, final Cursor cursor) { 
     super.bindView(view, context, cursor); 
     final TextView TextViewTitle = (TextView) view.findViewById(R.layout.activity_main); 

     Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/PangLong.ttf"); 
     TextViewTitle.setTypeface(customFont);       
    } 
} 

} 
+1

您還沒有叫'CustomFontAdapter'但'SimpleCursorAdapter'! – 2014-10-27 10:37:53

+0

檢查這個問題:[使用自定義simpleCursorAdapter](http://stackoverflow.com/questions/17708971/using-custom-simplecursoradapter) – 2014-10-27 10:40:54

回答

-1

你這是怎麼設置自定義字體,如果你在你的資產文件夾中的文件名爲.ttf。

//Set font style 
    Typeface tf; 
    // Font path 
    //public static String fontPath = "fonts/TELE2.TTF"; 
    public static String fontPath = "fonts/SketchRockwell-Bold.ttf"; 
    TextView tv_title; 

//中的onCreate():

tv_title = (TextView)findViewById(R.id.login_tv_title); 
     //Set font style 
     tf = Typeface.createFromAsset(getAssets(), fontPath); 
     tv_title.setTypeface(tf); 
+0

希望你已經檢查完他的問題!他已經在'CustomFontAdapter'類中使用了相同的代碼。 – 2014-10-27 10:36:57

+0

感謝您的回答。但之前還是一樣。 – 2014-10-27 13:59:11

+0

檢查@PareshMayani給出的鏈接。 – Akhil 2014-10-28 04:29:14