2017-02-22 21 views
0

私はそれがクリックされたときにクリックされたGridViewの項目の背景色を変更してから、私はときにしたいXamarinアンドロイド:

通常の色に戻る方法を探していますをクリックしたときにGridView項目の背景色を変更するIクリックすると、私のgridviewアイテムの背景色はオレンジ色になり、その後短時間の間背景は再び白です。ここで

は、私が発見したが、「デバイス」が知られていないものです。 )

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作成:

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

私はこの試み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

それはXamarin.FormsなくネイティブXamarinでのみ利用可能ですので、あなたは、Device -Classを使用することはできません。

しかし、あなたが戻っていくつかの時間後の色を変更するには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上で何かを変更するには、UIスレッドでこれを行う必要があります。

+0

... – 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))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に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.xmlcolors.xmlを作成することによって色を定義する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

グリッドビュー内の項目は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:それはeather働いていない理由を私は知らない

<?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

関連する問題