如何输入带有自定义字体的文本,将其放到SQLite中,然后在textview中显示?
我可以设置EditText和TextView的字体,但我想从EditText中获取字体并将其放入TextView(在我的例子中是ListView中的文本)
您可以将我的应用视为具有自定义字体的聊天应用。
由于
答案 0 :(得分:0)
好的,首先,字体与数据库没有任何关系。所以我指点你正确的方向来帮助你:
这是一个关于如何保存到数据库的教程:http://www.vogella.com/tutorials/AndroidSQLite/article.html(阅读它,它是一个很好的。)
此外,一旦您想要将数据显示在TextView
那么您应该将字体设置为TextView
:Android - Using Custom Font
答案 1 :(得分:0)
根据您的描述,我正在想象一个代表聊天线程/日志的ListView。 ListView中的每个项目(在本例中为TextView)表示单个消息。聊天线程中的每条消息都可以有自定义字体。您希望将消息的字体类型保留到数据库中。基本上,您希望使用自定义适配器更改TextView的字体。
为此,我将创建一个Message
对象。这个Message
对象会在其上有字段(即变量),如MessageContent
,MessageFont
等。然后,您可以将此对象持久保存到数据库中。从数据库中检索后,您可以使用自定义适配器将字体分配给TextView。
public class MessageCursorAdapter extends CursorAdapter {
private Cursor messageCursor;
private Context context;
private final LayoutInflater inflater;
public MessageCursorAdapter(Context context, Cursor cursor) {
super(context, cursor);
this.inflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView messageTextView = (TextView) view.findViewById(R.id.message_item_text);
String messageFont = cursor.getString(cursor.getColumnIndex("name_of_database_column"));
if (messageFont.equals("Epimodem")) {
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/epimodem.ttf");
messageTextView.setTypeface(face);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = this.inflater.inflate(R.layout.message_item, parent, false);
return view;
}
}
答案 2 :(得分:0)
在您扩展以填充数据的任何适配器类中,您将获得名为getView (int position, View convertView, ViewGroup parent)的函数。你想要什么就可以在这里完成。
您需要做的是创建一个扩展适配器的类,然后为您要返回并希望显示的视图充气,然后在TextView上设置TypeFaces。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Set the TypeFace Here
Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf");
textView.setTypeface(font);
return rowView;
}