0

私のアプリに次のようなフッターがあり、footer.xml などの XML ファイルで定義されているとします。

 <LinearLayout android:id="@+id/llfooter"
        android:layout_weight="1" android:layout_width="fill_parent"
        android:orientation="horizontal" android:layout_height="0dp"
        android:visibility="visible" android:background="@drawable/fbg"
        android:weightSum="5.0" android:gravity="center"
        android:layout_margin="0dp">

        <Button android:id="@+id/home" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/home" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/issue" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@android:drawable/ic_menu_send" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/browse" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@android:drawable/ic_menu_slideshow" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:id="@+id/search" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:layout_weight="1"
            android:background="@drawable/search" android:textColor="#FFFFFF"
            android:padding="10px"></Button>

        <Button android:layout_height="wrap_content" android:id="@+id/favorite"
            android:background="@drawable/favorite" android:layout_width="wrap_content"
            android:focusable="true"
            android:clickable="true"
            android:layout_weight="1"
            android:textColor="#FFFFFF" android:padding="10px"></Button>
    </LinearLayout>

ここでの問題は、homeissueBrowseなどは PNG アイコンであり、それらをタップすると、ユーザーは変更されないため、タッチのフィードバックを受け取ることができないことです。

それらを押すと背景色を変更したいと思います(たとえば、少し明るくします)。次のように、XML drawable () をボタンごとに 1 つ書き留めることができることを知っています。

  <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:state_focused="false" android:state_pressed="true" 
            android:drawable="@drawable/bgalt" /> 
      <item android:drawable="@drawable/bgnorm" /> 
  </selector>

..しかし、10 個のボタン (たとえば、フッター用に 5 個、ヘッダー用に 5 個) がある場合は、背景を変更した別の 10 個のボタンを作成する必要があります (したがって、グラフ エディターでの作業が増え、ラスター アイコンが増えるため、.apk が重くなります..)。リソースの機能ごとに1つのアイコンのみを使用して、代わりに明るい色の「onClick」と通常の色の「onRelease」を(Javaでも)作成する方法はありますか?

助言がありますか?

事前にTnx。ガボ

4

2 に答える 2

0

コードのonTouchでボタンビットマップを取得して色を変更することはできますが、それは悪い考えです。セレクターが最善の解決策です。

于 2012-08-06T22:39:05.193 に答える
0

ImageButton を使用し、android:src パラメータを背景が透明なボタン ドローアブルに設定し、次に android:background 値をセレクタ ドローアブルに設定します。たとえば、選択すると色が変わります。

そうすれば、アイコン用のドローアブルのセットと、ボタンの状態に応じて変化する背景用のドローアブルが 1 つあります。

于 2012-08-06T22:37:47.757 に答える