我的應用程序生成2個不同的列表,其中一個使用默認ListView
樣式,因爲ListView
行不包含TextView
。另一個列表使用自定義CursorAdapter
並在每行內部有一個TextView
。我想要做的就是讓兩個列表的邊距和文字大小完全相同。在ListView中創建TextView使用默認的ListView行樣式
我的第一個列表使用標準ListView
沒有TextView
每一行內,這裏是什麼樣子:
這裏是產生它的代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.records);
DatabaseHandler db = new DatabaseHandler(this);
ArrayList<String> records = db.getRecords(this);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, records));
}
這裏是它的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
我的第二個名單是使用帶有TextView
每一行內,其通過自定義的`CursorAdapter的生成ListView
,這裏是什麼樣子:
這裏是產生它的代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.achievements);
DatabaseHandler db = new DatabaseHandler(this);
Cursor cursor = db.getAchievements(this);
AchievementAdapter cursorAdapter = new AchievementAdapter(this, cursor);
this.setListAdapter(cursorAdapter);
}
private class AchievementAdapter extends CursorAdapter {
public AchievementAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public void bindView(View v, Context context, Cursor cursor) {
TextView tv = (TextView) v.findViewById(R.id.achView1);
if(cursor.getString(cursor.getColumnIndex("completed")).equals("yes")) {
tv.setText(cursor.getString(cursor.getColumnIndex("name"))+" (completed)");
tv.setTextColor(Color.GREEN);
}
else {
tv.setText(cursor.getString(cursor.getColumnIndex("name")));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.achievements_item, parent, false);
return v;
}
}
這裏是它的成就xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""/>
</LinearLayout>
這是它的achievement_item xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/achView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="" />
</LinearLayout>
基本上我只是想讓這兩個列表看起來完全一樣。有沒有辦法做到這一點,使TextView
繼承默認的ListView
行樣式?或者我將不得不自己玩邊際和一切?
也可以爲列表項創建自己的樣式,該列表項具有android.R.layout.simple_list_item_1的父項,否? –
@androiddeveloper你可以將上面的屬性組合在一個樣式中,但不能使用'android.R.layout.simple_list_item_'作爲* parent *,因爲該文件是一個佈局文件而不是樣式。 – Luksprog
哎呀,你是對的。我沒有注意到這一點。對不起。 –