2009-08-02 6 views
156

TextViewListViewsimple_list_item_1のように振る舞います。 XMLは次のとおりです。Androidセレクター&テキストの色

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:gravity="center" android:focusable="true" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:background="@android:drawable/list_selector_background" /> 

すべては、フォーカスされた状態で変更されていないことが予想されるテキストの色を除いてすべて機能します。 textAppearanceLargeInverseに変更するにはどうすればよいですか?

+0

誰かがxmlファイルを探している場合は、ここにあります: http://developer.android.com/resources/samples/Home/res/color/bright_text_dark_focused.html – swinefeaster

答えて

69

セレクタもここでの回答です。ソースでbright_text_dark_focused.xmlため

検索は、解像度/色のディレクトリの下にプロジェクトに追加し、その後

android:textColor="@color/bright_text_dark_focused" 
+4

これは "primary_text_dark_focused.xml"に変更されたと思います。 – greg7gkb

+10

これはちょうど私の心を吹き飛ばした。今回は、テキストの色を変更するためにリスナーを使って処理してきました。床に顎。ブリリアント! –

+0

ありがとう!ちょうど私が探していたもの! – Zzokk

0

setOnFocusChangeListenerを試しましたか?ハンドラ内で、テキストの外観を変更することができます。例えば

TextView text = (TextView)findViewById(R.id.text); 
text.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      ((TextView)v).setXXXX(); 
     } else { 
      ((TextView)v).setXXXX(); 
     } 
    } 
}); 

あなたはそれがフォーカスかいないとき、あなたが好きな変更適用することができます。また、ViewTreeObserverを使用して、グローバルなフォーカス変更を待機することもできます。例えば

View all = findViewById(R.id.id_of_top_level_view_on_layout); 
ViewTreeObserver vto = all.getViewTreeObserver(); 
vto.addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() { 
    public void onGlobalFocusChanged(
     View oldFocus, View newFocus) { 
     // xxxx 
    } 
}); 

私はこのことができますまたはあなたのアイデアを与える願っています。

+1

私はこの方法ではなくテキスト色は何らかの理由で変わらなかった。 – yanchenko

351
1がそう、働いていたまで、私はいくつかのテストを行うことによって得

としてのTextViewから参照してください: 解像度を/color/button_dark_text.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:color="#000000" /> <!-- pressed --> 
    <item android:state_focused="true" 
      android:color="#000000" /> <!-- focused --> 
    <item android:color="#FFFFFF" /> <!-- default --> 
</selector> 

RES /レイアウト/ view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="EXIT" 
     android:textColor="@color/button_dark_text" /> 
</LinearLayout> 
+7

:このセレクタXML – neufuture

+4

@neufutureで描画可能なアプリがクラッシュ:の、textColorは=「@カラー/ button_dark_text」 ' – Paul

+1

色の状態のリストを記述した文書があります:あなたは'アンドロイドを使用する必要があります。http://好奇心として – Rick77

26

は、ここで(少なくとも2.3に)リストに項目として正確に動作し、私の実装、

のres /レイアウト/ list_video_footer.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/list_video_footer" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@android:drawable/list_selector_background" 
     android:clickable="true" 
     android:gravity="center" 
     android:minHeight="98px" 
     android:text="@string/more" 
     android:textColor="@color/bright_text_dark_focused" 
     android:textSize="18dp" 
     android:textStyle="bold" /> 

</FrameLayout> 

のres /カラー/ bright_text_dark_focused.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_selected="true" android:color="#444"/> 
    <item android:state_focused="true" android:color="#444"/> 
    <item android:state_pressed="true" android:color="#444"/> 
    <item android:color="#ccc"/> 

</selector> 
+0

これは私のためによく働いて、私の場合には解像度の下にこのファイルを配置することを忘れないでください、私のTabHost実装 – oddmeter

3
です

セレクタの例を次に示します。 eclipseを使用している場合は、Ctrlキーを押しながらスペースをクリックしたときには何も表示されません。/入力する必要があります。あなたが参考のために見ることができ

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true" /> 
<item android:drawable="@drawable/btn_default_selected" 
    android:state_focused="true" 
    android:state_enabled="true" 
    android:state_window_focused="true" /> 
<item android:drawable="@drawable/btn_default_normal" /> 

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

2

私はいつもこの後、よりを検索せずに上記の溶液を使用していました。 ;-)

しかし、今日私は何かに出くわし、それを共有することを考えました。:)

この機能は、実際にAPI 1から入手可能であり、(我々はすでに知っているように)私たちはウィジェットのさまざまな状態に色を供給することができますColorStateList、と呼ばれています。

タブで使用してTextViewsこのセレクタの定義は(クラウスBalduinoのを試してみましたが、それはしませんでした)私のために働いた場合、それは、またhere.

1

非常によく文書化されている:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Active tab --> 
    <item 
    android:state_selected="true" 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:color="#000000" /> 

    <!-- Inactive tab --> 
    <item 
    android:state_selected="false" 
    android:state_focused="false" 
    android:state_pressed="false" 
    android:color="#FFFFFF" /> 

</selector> 
17

それを動作させるためには選択にリストビューに次のコードを使用し

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:color="#fff"/> 
    <item android:state_activated="true" android:color="#fff"/> 
    <item android:color="#000" /> 
</selector> 

を明らかにキーがstate_activated="true"状態です。

+6

のために強調表示/非強調色を調整するために非常によく働い/カラーフォルダ –

関連する問題