我有一个TextView
<TextView
android:background="@drawable/myshape"
android:padding="5dp"
... />
具有以下形状设置为背景:
<shape>
<corners android:radius="4dp" />
<solid android:color="#FFFAD6" />
</shape>
显示TextView
在ListView
. 问题是填充在滚动时丢失了。当列表视图首次呈现时,它看起来很好:
当列表来回滚动时,填充丢失:
我错过了什么?
编辑这是代码:
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
框保持其大小。
但是,我还没有解决方案。接受建议。