2015-09-08 32 views
0

使用Android Studio 1.3 - 基於一個在線教程 - 我在我的android手機上成功運行這個應用程序,但我沒有成功嘗試超過六種不同的ListView和TextView配置,有些來自這個優秀的計算器站點,以嘗試獲取自定義字體以在.xml中顯示文本。儘管我使用的是紮根Android 4.3進行調試和運行,但我最終需要使用自定義字體來修改和分發此特定應用程序,因爲在有根和無根的手機和設備上的oem字體缺少本地語言字符的全部內容。我已經使用Google開源字體制作了字體。 僅使用下面的MainActivity.java和row_site.xml,是否可以修改textView以使用它將顯示的自定義字體,我將它放在/ assets/fonts文件夾中,或者它會有幫助我包含這個項目的其他文件?是否可以爲自定義字體TextView配置此應用程序?

MainActivity

package com.example.stacksites; 

import java.io.FileNotFoundException; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Typeface; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private SitesAdapter mAdapter; 
    private ListView sitesList; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.i("StackSites", "OnCreate()"); 
     setContentView(R.layout.activity_main); 

     //Get reference to our ListView 
     sitesList = (ListView)findViewById(R.id.sitesList); 

     //Set the click listener to launch the browser when a row is clicked. 
     sitesList.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int pos,long id) { 
       String url = mAdapter.getItem(pos).getLink(); 
       Intent i = new Intent(Intent.ACTION_VIEW); 
       i.setData(Uri.parse(url)); 
       startActivity(i); 

      } 

     }); 

     /* 
     * If network is available download the xml from the Internet. 
     * If not then try to use the local file from last time. 
     */ 
     if(isNetworkAvailable()){ 
      Log.i("StackSites", "starting download Task"); 
      SitesDownloadTask download = new SitesDownloadTask(); 
      download.execute(); 
     }else{ 
      mAdapter = new SitesAdapter(getApplicationContext(), -1, SitesXmlPullParser.getStackSitesFromFile(MainActivity.this)); 
      sitesList.setAdapter(mAdapter); 
     } 

    } 

    //Helper method to determine if Internet connection is available. 
    private boolean isNetworkAvailable() { 
     ConnectivityManager connectivityManager 
       = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
     return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
    } 

    /* 
    * AsyncTask that will download the xml file for us and store it locally. 
    * After the download is done we'll parse the local file. 
    */ 
    private class SitesDownloadTask extends AsyncTask<Void, Void, Void>{ 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      //Download the file 
      try { 
       Downloader.DownloadFromUrl("http://example.com/stacksites.xml", openFileOutput("StackSites.xml", Context.MODE_PRIVATE)); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result){ 
      //setup our Adapter and set it to the ListView. 
      mAdapter = new SitesAdapter(MainActivity.this, -1, SitesXmlPullParser.getStackSitesFromFile(MainActivity.this)); 
      sitesList.setAdapter(mAdapter); 
      Log.i("StackSites", "adapter size = "+ mAdapter.getCount()); 
     } 
    } 

} 

row_site.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" > 

    <ProgressBar 
     android:id="@+id/progress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/iconImg" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_marginRight="8dp" /> 

    <TextView 
     android:id="@+id/nameTxt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/iconImg" 
     android:textSize="16sp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/aboutTxt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/iconImg" 
     android:textSize="12sp" 
     android:layout_below="@id/nameTxt" 
     /> 
+0

請檢查https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb如果幫助 – Raghunandan

+0

謝謝!我看過你慷慨指向的網站。在哪個文件中,以及在哪裏放置:「@BindingAdapter({」bind:font「}) public static void setFont(TextView textView,String fontName){ .getContext()。getAssets(),「fonts /」+ fontName)); }「 –

回答

0

你可以把你的自定義字體到資產然後嘗試使用Typeface.createFromAsset()

然後TextView.setTypeface()通過創建字體

相關問題