Android:我应该以哪种方式将代码与样式分开?

时间:2012-05-02 16:24:24

标签: android styles

开始Android编程,来自html / php / css,我在网上搜索了一种简单的方法将我的代码与我的风格分开 - 现在我需要在列表或表视图中显示数据库中的数据。

简单地说,我从数据库中获取一个游标,遍历它,在代码中动态创建每个列表项作为TextViews。然后我想将外部xml布局文件中的样式应用到每个项目。

的伪代码:

style.xml:

// mystyle:bold,12pt

// 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很快变得臃肿。我只想要一个很好的方法将样式应用于动态创建的代码。这可能吗?

1 个答案:

答案 0 :(得分:0)

如果您正在使用ListView,那么为行创建XML文件非常简单。您唯一需要的是XML文件和Adapter类。看看这个简单的例子:

要从数据库中读取数据,请创建一个这样的辅助类:

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) {
        }
    }
}

在您的Activity类中:

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类:

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):

<?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>