textview の背景画像と文字色をタップ時に変更する方法について

4,528 views
Skip to first unread message

ロウキル

unread,
Apr 19, 2012, 11:58:19 PM4/19/12
to android-g...@googlegroups.com
お世話になっております。
背景画像と文字色をタップ時と非タップ時で変更したいと考えております。
xmlでselectorを用意して以下のように設定したのですが、文字色は変わりますが
背景が表示されません。
android:textColorを削除すると画像がちゃんと表示されるのですが・・・

タップ時と非タップ時で背景画像と文字色を変更する方法を教えて頂きたいです。
よろしくお願い致します。

------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<item
           android:state_pressed="true"
           android:textColor="#ffffff"
           android:drawable="@drawable/btn_editer_menu_sel" />
<item
  android:textColor="#ffffff"
     android:drawable="@drawable/btn_editer_menu"/>
</selector>
------------------------------------------------------------------------------------------------------

MORIHIRO

unread,
Apr 21, 2012, 7:39:32 AM4/21/12
to android-g...@googlegroups.com
selectorはdrawableとかcolorとかのリソース定義で使用して、
それらのリソースを "android:background" とか "android:textColor" に
指定すればいいと思いますよ。

http://www.techdoctranslator.com/android/guide/resources/available-resources/color-list-resource#selector-element
http://www.techdoctranslator.com/android/guide/resources/available-resources/drawable-resource#StateList


2012年4月20日金曜日 12時58分19秒 UTC+9 ロウキル:

Androyer

unread,
May 7, 2012, 8:58:15 AM5/7/12
to 日本Androidの会
こんにちは。androidを学習中の者です。
ロウキルさんのお悩みを題材にして、私なりにxmlを学習・作成してみました。

MORIHIROさんが指摘するリンク先の情報を読みますと(私の理解が正しければ)colorとdrawはxmlを分けなければならないようです。

そこで、私は、各々に対応するTextViewとImageViewを設け、それらをRelativeLayoutで合体させてみました。
私の事例では2個のViewを合体させましたが、何か1個のViewで間に合うのであれば、そういった例を教えて頂ければ幸いです。

私が作成した3個のxmlを下記のとおり掲載しておきます。

1.ファイル名:res>layout>main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:clickable="true"
>

<!-- 画像を背景にする -->
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:contentDescription="@string/hello"
android:duplicateParentState="true"
android:src="@drawable/drawselector"
/>

<!-- 文字列を手前にする -->
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/hello"
android:duplicateParentState="true"
android:textColor="@color/colorselector"
/>

</RelativeLayout>

2.ファイル名:res>color>colorselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#ff0" android:state_pressed="true" />
<item android:color="#f0f" />
</selector>

3.ファイル名:res>drawable>drawselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_launcher"
android:state_pressed="true" />
<item android:drawable="@drawable/icon" />
</selector>

On 4月21日, 午後8:39, MORIHIRO <morihiro.s...@gmail.com> wrote:
> selectorはdrawableとかcolorとかのリソース定義で使用して、
> それらのリソースを "android:background" とか "android:textColor" に
> 指定すればいいと思いますよ。
>
> http://www.techdoctranslator.com/android/guide/resources/available-re...http://www.techdoctranslator.com/android/guide/resources/available-re...

MORIHIRO

unread,
May 7, 2012, 9:55:06 AM5/7/12
to android-g...@googlegroups.com
"android:background" を指定すれば1個のTextViewのみで可能ですよ。


2012年5月7日月曜日 21時58分15秒 UTC+9 Androyer:

Androyer

unread,
May 7, 2012, 1:34:13 PM5/7/12
to android-g...@googlegroups.com
MORIHIRO さん。早速のご回答を頂きありがとうございました。

main.xmlを下記のとおりにしますと、ViewはTextViewが1個だけで済みました。

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:clickable="true"
    android:gravity="center"
    android:text="@string/hello"
    android:textColor="@color/colorselector" 
    android:background="@drawable/drawselector"
    >
</TextView>

2012年5月7日月曜日 22時55分06秒 UTC+9 MORIHIRO:
2012年5月7日月曜日 22時55分06秒 UTC+9 MORIHIRO:

ロウキル

unread,
May 9, 2012, 11:57:02 PM5/9/12
to android-g...@googlegroups.com
MORIHIRO 様
Androyer 様

ご回答頂きましてありがとうございます。
ご返信が遅くなってしまい大変申し訳ありません。
上記方法で実現出来たのですが、ソースでnew した場合に正常に動作出来ませんでした。

TextView tvEnd = new TextView(m_csActivity);
tvEnd.setText("確  定");
tvEnd.setClickable(true);
tvEnd.setBackgroundResource(R.drawable.drawselector);
tvEnd.setTextColor(m_csActivity.getResources().getColor(R.color.colorselector));
tvEnd.setOnClickListener(this);

setの仕方に問題があるかと思いましたが、原因が分かりませんでした。
追加の質問となってしまい申し訳ありませんが、よろしくお願い申し上げます。


2012年5月8日火曜日 2時34分13秒 UTC+9 Androyer:

Androyer

unread,
May 10, 2012, 4:13:08 AM5/10/12
to android-g...@googlegroups.com
ロウキルさん。こんにちは。

ご質問の趣旨が「xmlで作成したレイアウトを表示したい」のであれば、それを実現するJavaのコードは次のとおりになります。
この場合、TextViewをnewする必要はありません。

public class SelectorActivity extends Activity implements OnClickListener {
    TextView tv;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.TextView);
        tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v==tv){//クリックした場合
            //業務を行う
            return;
        }
    }
}

この場合、main.xmlにおいて、TextViewに「android:id="@+id/TextView"」を付加する必要があります。

ロウキルさんのコードを見ると、JavaでTextViewをnewしていますので、「main.xmlを使わずに、Javaのコードで表示したい」ということになります。
この場合のJavaのコードは次のとおりになります。

public class SelectorActivity extends Activity implements OnClickListener {
    TextView tv;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ColorStateList csl = null;
        
        //文字の色を設定する。
        Resources r = getResources();
        XmlResourceParser xrp = r.getXml(R.color.colorselector); 
        try{
            csl = ColorStateList.createFromXml(r, xrp);
        }
        catch(IOException eIO){
        }
        catch(XmlPullParserException eXPP){
        }
        
        //TextViewを設定する。
        tv = new TextView(this);
        ViewGroup.LayoutParams VL = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(VL);
        tv.setText("確定");
        tv.setTextColor(csl);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundResource(R.drawable.drawselector);
        tv.setClickable(true);
        tv.setOnClickListener(this);
        
        //画面に表示する。
        LinearLayout ll = new LinearLayout(this);
        ll.addView(tv);
        setContentView(ll);
    }

    @Override
    public void onClick(View v) {
        if(v==tv){//クリックした場合
            //業務を行う
            return;
        }
    }
}


2012年5月10日木曜日 12時57分02秒 UTC+9 ロウキル:

ロウキル

unread,
May 10, 2012, 7:16:47 AM5/10/12
to android-g...@googlegroups.com
Androyer 様

お世話になっております。
ご回答頂きまして本当にありがとうございます。

ご回答頂いた方法で実現することが出来ました。
XmlResourceParser クラスの存在は知りませんでした。
新しいことが学べて嬉しいです。

ご多忙の中、丁寧にご対応頂きまして本当にありがとうございました。
今後共どうぞよろしくお願い申し上げます。

2012年5月10日木曜日 17時13分08秒 UTC+9 Androyer:
Reply all
Reply to author
Forward
0 new messages