2012-09-27 50 views
0

我有一個擴展ListFragment的Fragment,它在運行時添加到FrameLayout。 FrameLayout已經包含一個ImageView。如何從ListFragment中刪除透明度

當在運行時添加ListFragment時,我可以看到背景,我只想要一個純色。

main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout android:id="@+id/list_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/icon" 
     android:tileMode="repeat" /> 

</FrameLayout> 

我曾嘗試以下,但這只是讓他走黑,當我滾動:

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    getListView().setCacheColorHint(R.color.list_background); 
    super.onActivityCreated(savedInstanceState); 
} 

而在R.color.list_background是:

<color name="list_background">#e2f4c7</color> 

見下圖:

enter image description here

這裏是我的自定義適配器

public class DailyFlightsAdapter extends ArrayAdapter<DailyFlightVO> { 

Context context; 
int layoutResourceId;  
ArrayList<DailyFlightVO> data = null; 

public DailyFlightsAdapter(Context context, int layoutResourceId, ArrayList<DailyFlightVO> dailyFlights) { 
    super(context, layoutResourceId, dailyFlights); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    this.data = dailyFlights; 
} 

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

    if(row == null){ 
     LayoutInflater inflator = ((Activity)context).getLayoutInflater(); 
     row = inflator.inflate(layoutResourceId, parent, false); 

     holder = new FlightHolder(); 
     holder.distance = (TextView)row.findViewById(R.id.distance); 
     holder.pilot = (TextView)row.findViewById(R.id.pilot); 

     row.setTag(holder); 
    }else{ 
     holder = (FlightHolder)row.getTag(); 
    } 

    DailyFlightVO vo = data.get(position); 
    holder.distance.setText(vo.getDistance()); 
    holder.pilot.setText(vo.getPilot()); 

    return row; 
} 

static class FlightHolder{ 
    TextView distance; 
    TextView pilot; 
} 
} 

回答

1

代替getListView().setCacheColorHint(R.color.list_background);嘗試以下操作:

getListView().setBackgroundColor(getResources().getColor(R.color.list_background)); 

也看到文檔的setBackgroundColor和。您必須在此使用getColor,因爲您需要將資源ID轉換爲真實顏色。

+1

更新您的答案 - getListView()。setBackgroundColor(getResources()。getColor(R.color.list_background));因爲這確實幫助我找到了正確的方法。 – Neil

+0

哦,是的,你是對的,從setBackgroundColor int是真正的顏色。 – theomega

0

您需要設置背景爲你的ListView吧EMS。

+0

感謝您的回答,您能否提供一個代碼示例? – Neil

+0

發佈您的適配器代碼。你使用哪種佈局的物品? – Leandros

+0

添加了我的自定義適配器,刪除了導入語句,layout是一個帶有兩個TextView組件的自定義LinearLayout。 – Neil