[英]Is It Possible For a Custom Font TextView To Be Configured for this App?
Using Android Studio 1.3 - and based on an online tutorial - I run this app successfully on my android phone, as it is, but I have unsuccessfully tried more than half a dozen different ListView and TextView configurations, some from this excellent stackoverflow site, in order to try to get a custom font to display the text within the .xml. 我使用Android Studio 1.3(并基于在线教程)按原样在Android手机上成功运行了该应用程序,但我尝试了六种以上的不同ListView和TextView配置,其中一些来自这个出色的stackoverflow网站,为了尝试获取自定义字体以在.xml中显示文本。 Although I'm using a rooted Android 4.3 to debug and run, I would need to eventually modify and distribute this particular app with a custom font because oem fonts on rooted and unrooted phones and devices lack the repertoire of the local language's characters.
尽管我使用的是根植的Android 4.3进行调试和运行,但最终仍需要使用自定义字体来修改和分发此特定应用程序,因为有根和无根手机和设备上的oem字体缺少本地语言字符的全部功能。 I've made the fonts already from Google open source fonts.
我已经使用Google开源字体制作了这些字体。 Using only the MainActivity.java and the row_site.xml below, is it possible for me to modify the textView to use the custom font that it appears I would place within and distribute from the /assets/fonts folder or would it be helpful if I include other files from this project?
仅使用MainActivity.java和下面的row_site.xml,我是否可以修改textView以使用将出现在/ assets / fonts文件夹中并从中分发的自定义字体,或者如果我使用该字体,这是否有帮助包括该项目的其他文件?
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 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"
/>
You can put your custom font into assets then try using Typeface.createFromAsset() 您可以将自定义字体放入资产中,然后尝试使用Typeface.createFromAsset()
Then TextView.setTypeface()
passing the created typeface 然后
TextView.setTypeface()
传递创建的字体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.