【问题标题】:Make a TextView inside a ListView use the default ListView row style使 ListView 中的 TextView 使用默认的 ListView 行样式
【发布时间】:2012-05-31 14:20:03
【问题描述】:

我的应用生成 2 个不同的列表,其中一个使用默认的 ListView 样式,因为 ListView 行中不包含 TextView。另一个列表使用自定义 CursorAdapter 并在每行内有一个 TextView。我要做的就是使两个列表的边距和文本大小完全相同。

我的第一个列表是使用标准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>

我的第二个列表是在通过自定义 `CursorAdapter 生成的每一行中使用 ListViewTextView,如下所示:

这是生成它的代码:

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

这是它的成就项目 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 android-layout android-listview textview


    【解决方案1】:

    只需从默认的ListView 布局android.R.layout.simple_list_item_1 复制TextView 的属性,并将它们用于您自己的行布局:

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

    另外,如果您想要自定义的 ListView 行布局是 TextView,您可以删除父 LinearLayout,这样布局中就不会有多余的 View

    【讨论】:

    • 也可以为列表项创建自己的样式,它的父项是 android.R.layout.simple_list_item_1 ,不是吗?
    • @androiddeveloper 您可以将上面的属性分组到一个样式中,但您不能将android.R.layout.simple_list_item_ 用作 parent,因为该文件是布局文件而不是样式。
    【解决方案2】:

    在results_item.xml 中为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="" />
    

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多