5

iOSでは、ボタンの背景を画像に設定した場合、ボタンを押すと、ボタンのコンテンツ全体(テキストを含む)がシャドウされます。Androidで同じ効果を達成できますか、それとも状態ごとに異なる画像を使用する必要がありますか?また、状態ごとに異なる画像を使用している場合でも、テキストをシャドウにする方法を教えてください。汚い方法はOnClickListener、ボタンにを設定し、押されたときにプログラムでテキストをシャドウイングすることですが、他の方法はありますか?

4

3 に答える 3

7

私はしばらくの間これの解決策を見つけようとしていましたが、何も見つからなかったので、すべての画像ボタンで機能するかなりきちんとした解決策を思いつきました。iOSと同じように。

  • 黒の10%透明な画像を作成し、PNGとして保存します。私はそれをbutton_pressed.pngと呼んでいます。この画像を使用できます、http://img84.imageshack.us/img84/7924/buttonpressed.png

  • 関連するものと呼ばれるドローアブルを作成します。私はそれを「button_pressed_layout.xml」と呼びます。

    <?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/button_pressed"  />
    </selector>
    
  • 次に、ボタン画像をLinearLayoutに配置し、次にButtonをLinearLayout内に配置します。Buttonで、button_pressed_layout.xmlを背景として使用します。

        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/button_image">
            <Button
                android:id="@+id/myButtonId"
                android:text="@string/Continue"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/button_pressed_layout"
                android:textColor="#FFF"
                android:textSize="16dp" >                   
            </Button>
        </LinearLayout>
    

それでおしまい!

于 2011-07-29T13:32:10.207 に答える
3

2番目のボタン画像を作成せずに押された状態を取得するために、ボタンのアルファを変更するOnTouchListenerを作成しました。これは最も美しいプレス状態ではありませんが、効果的な自動動作を提供します。

public class PressedStateOnTouchListener implements OnTouchListener
{
    PressedStateOnTouchListener( float alphaNormal )
    {
        mAlphaNormal    = alphaNormal;
    }

    public boolean onTouch( View theView, MotionEvent motionEvent )
    {
        switch( motionEvent.getAction() ) {
            case MotionEvent.ACTION_DOWN:
                theView.setAlpha( mAlphaNormal / 2.0f );
                break;

            case MotionEvent.ACTION_UP:
                theView.setAlpha( mAlphaNormal );
                break;
        }

        // return false because I still want this to bubble off into an onClick
        return false;
    }

    private float   mAlphaNormal;
}

アクティビティで、このリスナーを各ボタンに適用します。

Button theButton =  (Button)findViewById( R.id.my_button_id );
theButton.setOnTouchListener( new PressedStateOnTouchListener( theButton.getAlpha() ));
于 2013-02-28T15:40:14.910 に答える
0

ボタン自体にさまざまな効果を与えるには、ここに示すようにカスタム画像を使用する必要があると思います。詳細はここにありません。テキストに関しては、この投稿は影のトリックを行うように見えます

于 2011-06-17T00:37:42.180 に答える