我有一個TextView
的Android - 視圖失去填充上的ListView滾動
<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);
}
}
更新玩弄後,我發現了這種情況的發生,因爲文本拆分爲2行(使用<br>
或\n
):
box.setText(Html.fromHtml("<b>Foo</b><br><small>Bar</small>"));
box.setText("Foo \n Bar");
當我刪除<br>
或\n
時,TextView
框保留其大小即
但是我還沒有解決方案。接受建議。
如果將填充放入形狀中,該怎麼辦? –
試過了。同樣的結果,但... – armandino
你可以發佈一些更多的代碼,並且還提到你測試過的設備嗎? –