我是 android 新手...我想在我的 android 项目中使用自定义字体,所以我把我的代码库放在这个教程上。我想在 R.id.Headline 和一些 xml 元素中使用我的自定义字体
我的问题是:
- 我的代码有问题吗?
- 有没有从 xml 构建自定义 UI 的教程?
这是我的代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
private static String url = "http:some url";
private static final String TAG_HEADLINE = "headline";
private static final String TAG_PROVIDER = "provider";
private static final String TAG_TYPE = "deal_type";
private static final String TAG_BLURB = "blurb";
private static final String TAG_FEATURED = "featured";
private static final String TAG_URL = "url";
private static final String TAG_PRICE = "price";
private static final String TAG_LOCATION = "location";
private static final String TAG_IMAGE = "image";
private static final String TAG_OFFER = "offer";
private static final String TAG_THUMBNAIL = "thumbnail";
private static final String TAG_FORMATED = "formatted";
ArrayList<HashMap<String, String>> dealArrayList = new ArrayList<HashMap<String, String>>();
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
ArrayList<HashMap<String, String>> deals = new NetraAsycn()
.execute(url).get();
ListAdapter adapter = new SimpleAdapter(this, deals,
R.layout.list_item, new String[] { TAG_THUMBNAIL,
TAG_FORMATED, TAG_PROVIDER, TAG_LOCATION,
TAG_HEADLINE }, new int[] { R.id.thumbnail,
R.id.price, R.id.provider, R.id.location,
R.id.headline });
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
TextView myTextView = (TextView)findViewById(R.id.headline);
myTextView.setTypeface(myTypeface);
setListAdapter(adapter);
ListView lv = getListView();
lv.setDividerHeight(0);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.TextView01);
viewHolder.image = (ImageView) rowView
.findViewById(R.id.ImageView01);
rowView.setTag(viewHolder);
}
return rowView;
}
}