Android:应该以哪种方式将代码与样式分开? [英] Android: In which way should I separate code from style?
问题描述
从html/php/css开始的Android编程开始,我在网上搜索了一种简单的方法来将代码与样式分开-现在,我需要以列表视图或表视图显示数据库中的数据.
Beginning Android programming, coming from html/php/css, I have searched the web for a simple way to separate my code from my style - for now I need to display data from a database in a list- or table view.
简而言之,我从数据库中获取了一个游标,对其进行了遍历,并在代码中以TextViews的形式动态创建了每个列表项.然后,我想将外部xml布局文件中的样式应用于每个项目.
Simply put, I get a cursor from the database, iterate through it, creating each list-item dynamically in code as TextViews. Then I would like to apply a style from an external xml layout file to each item.
伪代码:
style.xml:
//mystyle:粗体,12pt
//mystyle: bold, 12pt
//您的样式:斜体,11pt
//yourstyle: italic, 11pt
活动:
for (each cursor-entry)
{
tv1 = new TextView();
applyStyle(tv, mystyle);
tv2 = new TextView();
applyStyle(tv, yourstyle);
//Apply content to textviews from the cursor...
}
mainLayout.setView(tv1);
mainLayout.setView(tv2);
我在网上发现的代码示例使用多行代码或多个xml文件(使用inflate或cursorAdapters),并且IMO很快变得肿.我只想要一种将样式应用于动态创建的代码的好方法.这可能吗?
The code examples I've found around the net, uses multiple lines of code, or multiple xml files (using inflate, or cursorAdapters), and IMO quickly become bloated. I just want a nice neat way to apply a style to a dynamically created code. Is this possible?
推荐答案
如果您使用的是ListView,那么拥有一个用于行的XML文件非常简单.您唯一需要的是XML文件和Adapter类.看一下这个简单的例子:
If you are using ListView, it is so simple to have an XML file for rows. The only thing you need is an XML file and a Adapter class. Take a look at this simple example:
要从数据库读取数据,请创建一个如下的帮助器类:
To read data from database, create a helper class like this:
public class MessagingDatabaseAdapter {
protected SQLiteDatabase database;
public MessagingDatabaseAdapter(Context context) {
MessagingDatabaseHelper databaseHelper = new MessagingDatabaseHelper(context, "message_history_db");
database = databaseHelper.getWritableDatabase();
}
public void close() {
database.close();
}
public void Entity[] getAllEntities() {
Entity[] values = null;
String query = "select * from TABLE_NAME";
Cursor cursor = null;
try {
cursor = database.rawQuery(query, null);
if( cursor.moveToFirst() ) {
int s = cursor.getCount();
values = new Entity[s];
do {
Entity entity = new Entity();
entity.setSomeProperty(cursor.getInt(cursor.getColumnIndex(SOME_PROPERTY_COLUMN)));
values[i++] = entity;
} while( cursor.moveToNext() );
}
} catch(Exception ex) {
} finally {
if( cursor != null ) {
cursor.close();
}
return values;
}
}
protected class MessagingDatabaseHelper extends SQLiteOpenHelper {
public MessagingDatabaseHelper(Context context, String name) {
super(context, name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Your SQL to create Tables");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
在活动"课程中:
MessagingDatabaseAdapter db = new MessagingDatabaseAdapter();
values = db.getAllEntities();
db.close();
list_view = (ListView) findViewById(R.id.list_view);
ListAdapter adapter = new ListAdapter(this, values);
list_view.setAdapter(adapter);
和ListAdapter类:
And ListAdapter class:
public class ListAdapter extends ArrayAdapter<Entity> {
final Context context;
final Entity[] values;
public ListAdapter(Context context, Entity[] values) {
super(context, R.layout.list_screen, values);
this.values = values;
this.context = context;
}
@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.list_view_item, parent, false);
TextView datetimeTextView = (TextView) rowView.findViewById(R.id.list_view_datetime_text_view);
datetimeTextView.setTypeface(someTypeFace);
return rowView;
}
}
行布局XML文件(list_view_item.xml):
And row layout XML file (list_view_item.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/BorderedFrame" >
<TextView
android:id="@+id/inbox_list_view_datetime_text_view"
style="@style/MediumText"
android:layout_width="wrap_content" >
</TextView>
</RelativeLayout>
这篇关于Android:应该以哪种方式将代码与样式分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!