2012-12-10 55 views
2

我有一個TextView的Android - 視圖失去填充上的ListView滾動

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

具有以下形狀設置爲背景:

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

TextViewListView內顯示。問題在於滾動時填充已丟失。當列表視圖呈現第一它看起來不錯:

enter image description here

當列表滾動來回填充丟失:

enter image description here

我缺少什麼?

編輯下面的代碼:

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框保留其大小即

但是我還沒有解決方案。接受建議。

+0

如果將填充放入形狀中,該怎麼辦? –

+0

試過了。同樣的結果,但... – armandino

+0

你可以發佈一些更多的代碼,並且還提到你測試過的設備嗎? –

回答

1

我也面臨類似的行爲。當在視圖列表適配器中回收視圖時,它看起來像佈局參數會丟失。我已經在getView()我的適配器的方法中設置了佈局參數。您可以在getView()中嘗試setPadding(int left,int top,int right,int bottom)並查看它是否解決了問題。

+0

謝謝。我會試一試。 – armandino

+0

不...沒有工作。 – armandino