2017-01-23 3 views
0

私は2つの画像を持っています。一つは数字、もう一つは影です。 2枚目の写真に見られるように、この2枚の画像を表示する必要があります。ボタンが押され、2つの画像xml android

これは私のコードです。今すぐ表示されるのは数字だけです(数字が押されていない場合)、または影(押されている場合)です。

ユーザーがボタンを押したときに両方の画像を表示するにはどうすればよいですか?

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

    <item android:drawable="@drawable/button_select" android:state_pressed="true" 
     android:alpha="0.75"/> 
    <item android:drawable="@drawable/number_1" android:state_enabled="true" /> 

    <item android:drawable="@drawable/number_1" android:state_pressed="false" 
     /> 
</selector> 

this are the two images

this is the goal

また、私はこの方法でlayerDrawableを使用しようとしていたが、うまくいきませんでした:

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

    <item android:drawable="@drawable/button_select" android:state_pressed="true" 
     android:alpha="0.75"/> 
    <item android:drawable="@drawable/number_1" android:state_enabled="true" /> 
</selector> 

</item> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/number_1" android:state_pressed="false" 
     /> 
</selector> 
</layer-list> 
+0

てください画像。 –

+0

@AbhriyaRoy申し訳ありません、今ははい – baldcl

答えて

0

あなたは2枚の異なる画像...

を使用する必要があります
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/pressed_image" android:state_pressed="true"/> 
    <item android:drawable="@drawable/normal_image" android:state_pressed="false"/> 
</selector> 
+0

私は知っています、ありがとうございますが、私は2つの画像を表示する必要があり、1つが偽である場合は、私はフォローする必要があるモデルで画像をアップロードします。 – baldcl

+0

ボタンを押したときに2つの画像をどのように表示したいのですか? –

+0

レイヤーリストを使用してもう1つのドロウアブルを作成する必要があります。それは動作します。 –

0

あなただけiv_img番号の画像GOOD LUCK

+0

gosaviありがとうございますが、私は使用する必要がありますそれはボタンのように、ユーザーのプレスは、現在の画像を変更する必要があります。 – baldcl

+0

あなたはこのコードでそれを行うことができます。android:id = "@ + id/iv_img"画像 これまでに何が欲しいのですか?セレクタdrawableをこの画像に設定してください:android:id = "@ + id/iv_background"ありがとうございます。 –

0

を変更すること

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/iv_background" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_select"/> 
    <ImageView 
     android:id="@+id/iv_img" 
     android:layout_width="wrap_content" 
     android:padding="5dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/number_1"/> 

</RelativeLayout> 

のための相対的なレイアウトを使用することができ、このコードで描画可能なセレクタファイルを変更してください。..

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true"> 
    <layer-list> 
     <item android:drawable="@drawable/number_1" /> 
     <item android:bottom="@dimen/margin_5" android:left="@dimen/margin_5" android:right="@dimen/margin_5" android:top="@dimen/margin_5"> 
      <bitmap android:src="@drawable/button_select" /> 
     </item> 
    </layer-list> 
</item> 
<item android:drawable="@drawable/number_1" android:state_pressed="false"/> 

制限::

それはあなたがそれぞれに異なるselector.xmlを作成する必要が複数の番号..for数の単一のイメージのために使用することができます

..

より詳細な説明が必要な場合は、教えたり助けてください..

+0

ありがとうございました!これは私が探していたことをする!私はボタンを押して2つの画像を表示すると、以前のものよりも大きくなり、同じ次元で "number_1"を維持する可能性がありますか? – baldcl

+0

この行の余白を必要に応じて変更してください。

関連する問題