2

我有一个TextView

<TextView
    android:background="@drawable/myshape"
    android:padding="5dp"
    ... />

具有以下形状设置为背景:

<shape>
    <corners android:radius="4dp" />
    <solid android:color="#FFFAD6" />
</shape>

显示TextViewListView. 问题是填充在滚动时丢失了。当列表视图首次呈现时,它看起来很好:

在此处输入图像描述

当列表来回滚动时,填充丢失:

在此处输入图像描述

我错过了什么?

编辑这是代码:

public class MyArrayAdapter extends ArrayAdapter<MyBean> {

    private Context context;
    private List<MyBean> myBeans;

    public MyArrayAdapter(Context context, int resource, int textViewResourceId, List<MyBean> myBeans) {
        super(context, resource, textViewResourceId, myBeans);
        this.context = context;
        this.myBeans = myBeans;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.my_activity, parent, false);
        }

        MyBean bean = myBeans.get(position);
        setData(row, bean);
        return row;
    }

    private void setData(View layout, MyBean bean) {
        TextView dateHeader = (TextView) layout.findViewById(R.id.dateHeader);
        dateHeader.setText(bean.getDate());

        TextView textView1 = (TextView) layout.findViewById(R.id.box);
        textView1.setText("textView 1");

        TextView box = (TextView) layout.findViewById(R.id.box);
        box.setText(Html.fromHtml("<b>Foo</b><br><small>Bar</small>"));
        // etc..
    }
}


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);

        ListView listView = new ListView(this);
        setContentView(listView);
        ArrayAdapter<MyBean> arrayAdapter = new MyBeanArrayAdapter(
                this, R.layout.my_activity, R.id.dateHeader, getBeans());

        listView.setAdapter(arrayAdapter);
    }

}

更新玩了之后,我发现这是因为文本被分成两行(使用<br>\n):

box.setText(Html.fromHtml("<b>Foo</b><br><small>Bar</small>"));
box.setText("Foo \n Bar");

当我删除<br>or\n时,TextView框保持其大小。

但是,我还没有解决方案。接受建议。

4

1 回答 1

1

我也遇到过类似的行为。当在列表适配器中查看回收时,布局参数似乎丢失了。我最终在我的适配器的getView()方法中设置了布局参数。您可以在 getView() 中尝试 setPadding (int left, int top, int right, int bottom) 并查看它是否解决了问题。

于 2012-12-10T20:30:46.903 回答