2016-05-17 103 views
0

我剛剛開始編程前不久,所以請裸露在我身邊。更改ListView中的項目的顏色和ClickEvent(Xamarin Android)

我面臨的問題是我無法將列表視圖中我的項目的顏色更改爲黑色。

我的ListView的背景(我已經刪除了,以便看到Listview中的項目)也應該是白色背景。

在我的活動中,我使用adapter.add(「Item」)添加了我的項目。因此,我失去了嘗試在該項上添加單擊事件,因爲它是一個字符串。

試過:

1)直接在axml文件中更改屬性。如android:textColor/Foreground等。

2)添加一個color.xml文件並聲明它的顏色。然後調用該顏色變量到我的列表視圖組件「android:thatcolorname」

3)試圖改變它從我的編碼內我的活動。如mLeftItems.Foreground/TextColor等甚至嘗試使用android.graphics.color等它不適合我。

我對我的列表視圖編碼是

 mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.myDrawer); 
     mLeftDrawer = FindViewById<ListView>(Resource.Id.leftListView); 

     mLeftItems.Add("Add Device"); 
     mLeftItems.Add("Disconnect Device"); 
     mLeftAdapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, mLeftItems); 
     mLeftDrawer.Adapter = mLeftAdapter; 

對於我.axml文件,它是

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myDrawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 
<!-- The left navigation drawer --> 
    <ListView 
     android:id="@+id/leftListView" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     android:choiceMode="singleChoice" 
     android:divider="#D2D2D2" 
     android:dividerHeight="2dp" /> 
</android.support.v4.widget.DrawerLayout> 

任何指導或幫助是非常讚賞。

回答

0

沒關係,我已經想通了。通過更改代碼

mLeftAdapter = new ArrayAdapter(this,Android.Resource.Layout.SimpleListItem1,mLeftItems);

mLeftAdapter =新ArrayAdapter(此,Android.Resource.Layout.newLayout,Resource.Id.YourComponentID,mLeftItems);

請注意,我創建了一個新的axml佈局來替換預先設置的SimpleListItem1,默認情況下它是白色文本。該佈局內應該包含一個按鈕或文本視圖。並從那裏,你可以改變你想要的按鈕/ textview組件的顏色。然後你在Resource.Id.Xxxxxxx上調用Id。

希望這可以幫助每個人在未來。

相關問題