2011-06-02 60 views
0

我必須爲項目做一個列表。這裏是我的XML佈局:Android listview高亮顯示問題

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center_horizontal" 
     android:background="#FFFFFF" 
     android:stretchColumns="0"> 
     <TableRow 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="center_horizontal" 
      android:id="@+id/listbox"> 
      <LinearLayout 
       android:layout_width="fill_parent" 
       android:background="#FFFFFF" 
       android:layout_gravity="center_horizontal" 
       android:layout_height="fill_parent"> 
        <ListView 
         android:id="@+id/android:list" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"> 
        </ListView> 
        <LinearLayout 
         android:id="@android:id/empty" 
         android:layout_width="fill_parent" 
         android:background="#FFFFFF" 
         android:layout_gravity="center" 
         android:layout_height="fill_parent"> 
         <TextView 
          android:id="@+id/emptytext" 
          android:paddingTop="100dp" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:textColor="#818185" 
          android:textSize="25sp" 
          android:textStyle="bold" 
          android:background="#FFFFFF" 
          android:text="@string/empty_fav" 
          android:gravity="center_horizontal|center_vertical" 
          android:paddingLeft="6dip" 
         /> 
        </LinearLayout> 
      </LinearLayout>   
     </TableRow> 
</TableLayout> 

這是R.layout.custom_list_item_1的xml。

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:background="#FFFFFF" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
/> 

這裏是我的代碼。

public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.fav_layout); 
     .... 
     ........ 

     wordDataHelper = new WordDataHelper(getApplicationContext()); 
     favCursor = wordDataHelper.getCursorFav(); 
     startManagingCursor(favCursor); 

     favAdapter = new SimpleCursorAdapter(
       this, 
       R.layout.custom_list_item_1, 
       favCursor,            
       new String[] {WordDataHelper.ENGWORD},   
       new int[] {android.R.id.text1}); 

     setListAdapter(favAdapter); 

     list = getListView(); 

     list.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
        long id) { 
       // TODO Auto-generated method stub 

       showVideo(((TextView) view).getText()); 

      }}); 

     list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { 
      // @Override 
      public void onCreateContextMenu(ContextMenu menu, View v, 
        ContextMenu.ContextMenuInfo menuInfo) { 
       menu.setHeaderTitle("Context Menu"); 
       menu.add(0, CONTEXT_DELETE, 1, "Delete Item"); 
      } 
     }); 

     list.setTextFilterEnabled(true); 

     list.setClickable(true); 
} 

現在一切都顯示正常,所有其他功能也是 工作正常。但是,如果我觸摸任何一行,則不突出顯示 ,即顏色不變。我的代碼中是否有任何問題或 是否必須單獨實施?如果是這樣,怎麼樣?

回答

0

你可能需要設置ListView

android:listSelector 

屬性。

檢查答案Android ListView Selector Color,它很好解釋。

如果沒有工作(我還沒有嘗試過),可以嘗試設置顏色選擇,如R.layout.custom_list_item_1

0

問題定義TextView的背景是,你重寫默認的背景可以繪製成純色,不支持狀態(點擊,聚焦等)。如果您希望背景爲白色,則最好使用其中一種「明亮」的Android主題,例如@android:style/Theme.Light.NoTitleBar

0

嘗試刪除您的TextView項的背景色:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
/>