2017-02-22 51 views
0

我正在尋找如何改變點擊的gridview的項目的背景顏色被點擊它,然後回到正常的顏色Xamarin的Android:點擊

我想,當,當更改一個GridView項的背景顏色我點擊,我gridview項目的背景顏色是橙色,然後很短的時間後,背景再次變成白色。

這是我發現的但「設備」未知。

e.View.SetBackgroundColor(Color.White); 
Device.StartTimer(TimeSpan.FromSeconds(0.25),() => 
{ 
    e.View.SetBackgroundColor(Color.Orange); 
    return false; 
}); 

我嘗試這樣做:

1)通過在值

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="pressed_color">#972234</color> 
<color name="default_color">#000000</color> 
</resources> 

2創建colors.xml定義顏色)創建在抽拉

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:state_selected="true" 
    android:drawable="@color/pressed_color"/> 
<item 
    android:state_pressed="true" 
    android:drawable="@color/pressed_color"/> 
<item 
    android:drawable="@color/default_color" /> 
</selector> 

3 bg_key.xml)將android:listSelector和listSelector設置爲GridView

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:columnWidth="90dp" 
     android:numColumns="auto_fit" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="columnWidth" 
     android:gravity="center" 

     android:listSelector="@drawable/bg_key" 
     android:background="@color/default_color" 

     /> 

它正在我的側面菜單上,但不是在我的gridview上......我的網格視圖是由ImageView和TextView組成的嗎?

此外,我應該改變(我的側邊菜單)改變字體顏色,而不是背景顏色?

回答

0

您不能使用Device-Class,因爲它只在Xamarin.Forms中可用,而不在本機中提供Xamarin。

但是你可以使用System.Timers.Timer類來改變顏色經過一段時間後回:

var t = new System.Timers.Timer(); 
t.Interval = 250; // In miliseconds 
t.Elapsed += (sender, args) => 
{ 
    // Change color back on the UI-Thread 
    RunOnUiThread(() => 
    { 
     e.View.SetBackgroundColor(Color.Orange);   
    }); 

}; 
t.Start(); 

重要提示:Elapsed - 活動是NOT invoked on the UI-Thread。所以要改變用戶界面上的東西(就像你的背景顏色),你需要在UI線程上做這件事。

+0

我不知道爲什麼,但它不eather工作... – Kurapika

+0

我的心靈感應能力都不甚理想......請解釋清楚你有什麼試過,發生了什麼事。否則,我們無法爲您提供幫助 – Joehl

+0

我嘗試過您發佈的內容並且顏色不變。我是Xamarin的新手,所以也許我沒有把它寫在好的地方,我寫在這裏:'gridView.ItemClick + =(s,e)=> var t = new System.Timers.Timer() ; t.Interval = 250; //在毫秒 t.Elapsed + =(發件人,參數)=> { RunOnUiThread(()=> { e.View.SetBackgroundColor(Color.Orange); }); }; t.Start(); };' – Kurapika

0

Device.StartTimer用於Xamarin.Forms使用設備時鐘功能啓動一個循環計時器。它不適用於本機。

我更喜歡你可以製作一個自定義樣式。


試試這個:

1)values

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="pressed_color">#972234</color> 
    <color name="default_color">#000000</color> 
</resources> 

2)創建bg_key.xml通過創建colors.xml定義顏色在drawable

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_selected="true" 
     android:drawable="@color/pressed_color"/> 
    <item 
     android:state_pressed="true" 
     android:drawable="@color/pressed_color"/> 
    <item 
     android:drawable="@color/default_color" /> 
</selector> 

3)設置android:listSelector和listSelector到格查看

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/gridview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:columnWidth="90dp" 
      android:numColumns="auto_fit" 
      android:verticalSpacing="10dp" 
      android:horizontalSpacing="10dp" 
      android:stretchMode="columnWidth" 
      android:gravity="center" 

      android:listSelector="@drawable/bg_key" 
      android:background="@color/default_color" 

      /> 
+0

它不工作eather,顏色不會改變...網格視圖中的項目由inageView和TextView組成,是它的問題嗎? – Kurapika

+0

因爲它在我的菜單項上工作,所以我猜它一定是因爲TextView和ImageView的權利? – Kurapika

+0

提供你的工作的一小部分,所以我可以通過它。 – Vaikesh

0

這裏是我的代碼:

主。axml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#EBEAEF"> 
<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:paddingLeft="5dp" 
    android:background="#282059" 
    android:title="test" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" /> 
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
<!-- The Main Content View --> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/logoBackgroud" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/icon_background" /> 
     <GridView 
      android:id="@+id/grid_view_image_text" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:columnWidth="350dp" 
      android:layout_margin="10dp" 
      android:gravity="center" 
      android:numColumns="auto_fit" /> 
    </RelativeLayout> 

gridview.axml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#EBEAEF"> 
<RelativeLayout 
    android:layout_height="90dp" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_margin="25dp" 
    android:listSelector="@drawable/bg_key" 
    android:background="#F4F4F6"> 
<!-- Letter yellow color = #FAB322 --> 
    <TextView 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_width="95dp" 
     android:layout_height="fill_parent" 
     android:textSize="66sp" 
     android:textColor="#0071CF" 
     android:background="@color/white" 
     android:layout_centerVertical="true" 
     android:layout_gravity="left" 
     android:gravity="center_vertical|center_horizontal" 
     android:text="A" 
     android:paddingBottom="5dp" 
     android:id="@+id/textViewLetter" /> 
    <TextView 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:paddingLeft="15dp" 
     android:layout_toRightOf="@+id/textViewLetter" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="35dp" 
     android:textSize="22sp" 
     android:gravity="center_vertical" 
     android:background="#F4F4F6" 
     android:textColor="#262057" 
     android:textStyle="bold" 
     android:listSelector="@drawable/bg_key" 
     android:id="@+id/textViewFileName" /> 
    <ImageView 
     android:layout_width="35dp" 
     android:layout_height="wrap_content" 
     android:paddingTop="5dp" 
     android:layout_alignParentRight="true" 
     android:id="@+id/imageViewIcon" /> 
</RelativeLayout> 
</LinearLayout> 
+0

@Vaikesh這裏是我的代碼:) – Kurapika