2016-04-19 9 views
0

私はAndroidのユーザーインターフェイスを開発することを学んでいます。私は異なるボタンの状態を示すdrawableのセットを持つImageButtonを作成したいと思います。私はボタンごとに2枚の画像を作成しました。通常の状態では133dp、押しボタン効果を作成するには拡大縮小コピー(126dp)です。ボタンサイズに合わせてImageButton srcを伸ばす必要はありません

enter image description here

Iは、画像セレクタ機構は、少なくとも機能することを示すために、第二の絵を描きました。

は、ここで私はボタンを押すと、それは予想通り色のみではなく、サイズを変更する私のアプリケーション

enter image description here

の初期状態です。両方の画像は同じサイズに装着されている

enter image description here

アクティビティレイアウトのxml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainUserActions" 
    android:background="@android:color/transparent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="3" 
     android:layout_alignParentTop="true"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:weightSum="2" 
      android:gravity="top"> 

      <ImageButton 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/imageButton" 
       android:clickable="true" 
       android:layout_weight="1" 
       android:src="@drawable/broom_stick_image_selection" 
       android:background="@null" 
       android:layout_gravity="center" 
       android:scaleType="center" /> 

      <ImageButton 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/imageButton2" 
       android:layout_gravity="center" 
       android:clickable="true" 
       android:layout_weight="1" 
       android:background="@null" 
       android:src="@drawable/vacuum_selection" 
       android:scaleType="center" /> 
     </LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1"></LinearLayout> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_gravity="center_horizontal" 
      android:layout_weight="1"></LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

描画可能選択のxml:システムに伝えるだけのsrcイメージが上の中央にどのよう

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:drawable="@drawable/broom_stick_white_pressed_rose" /> <!-- pressed --> 
    <item android:state_focused="true" 
      android:drawable="@drawable/broom_stick_white" /> <!-- focused --> 
    <item android:drawable="@drawable/broom_stick_white" /> <!-- default --> 
</selector> 

ボタンのレイアウトは、同じサイズに合わせるのではないのですか?

+0

ImageButtonsの幅と高さを 'wrap_content'に設定してみてください。 – NoChinDeluxe

答えて

1

これを達成する1つの方法は、オフセットの定義を可能にするlayer-listにアイテムをラップすることです。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <layer-list> 
      <item 
       android:drawable="@drawable/broom_stick_white_pressed_rose" 
       android:top="2dp" 
       android:right="2dp" 
       android:bottom="2dp" 
       android:left="2dp" /> 
     </layer-list> 
    </item> 
    <item android:drawable="@drawable/broom_stick_white" /> 
</selector> 
+0

私は今、追加イメージの必要がないことを見る - 元のイメージで十分です! –

関連する問題