Make a TextView inside a ListView use the default ListView row style

核能气质少年 提交于 2020-01-04 02:45:18

问题


My app generates 2 different lists, one uses the default ListView style as the ListView rows contain no TextView inside of them. The other list uses a custom CursorAdapter and has a TextView inside each row. All I want to do is make it so that the margins and text size are exactly the same for both lists.

My first list is using a standard ListView with no TextView inside each row, here is what it looks like:

Here is the code to generate it:

@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));
} 

Here is the xml file for it:

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

My second list is using a ListView with a TextView inside each row that is generated via a custom `CursorAdapter, here is what it looks like:

Here is the code to generate it:

@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;
    }
}   

Here is the achievements xml file for it:

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

Here is the achievements_item xml for it:

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

Basically I just want these 2 lists to look exactly the same. Is there a way to do it so that the TextView inherits the default ListView row style? Or am I going to have to play with the margins and everything myself?


回答1:


Just copy the attributes of the TextView from the default ListView layout android.R.layout.simple_list_item_1 and use them on your own row layout:

<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="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:minHeight="?android:attr/listPreferredItemHeight" />

</LinearLayout>

Also, if all you want for the customized ListView row layout is a TextView, you could remove the parent LinearLayout so you don't have an extra View in the layout




回答2:


In achievements_item.xml add some padding to the top and bottom of the TextView

 <TextView
        android:id="@+id/achView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="" />


来源:https://stackoverflow.com/questions/10835468/make-a-textview-inside-a-listview-use-the-default-listview-row-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!