0

実行時にセレクターから色を変更する必要があり、それを 4 つの異なるボタンに適用する必要があります。

ImageButton のコードは次のとおりです。

<ImageButton
   android:id="@+id/buttonAgenda"
   android:layout_width="100dp"
   android:layout_height="100dp"           
   android:background="@drawable/drawable_button_states_corners"
   android:src="@drawable/boton_agenda_1"/>

drawable_button_states_corners:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"> 
        <solid android:color="@color/galicia2"/>    
        <corners 
            android:bottomRightRadius="10dp"
            android:bottomLeftRadius="10dp" 
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp"/> 
    </shape>         
</item>
<item android:state_enabled="true">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"> 
        <solid android:color="@color/galicia"/>    
        <corners android:bottomRightRadius="10dp"
            android:bottomLeftRadius="10dp" 
            android:topLeftRadius="10dp"
            android:topRightRadius="10dp"/> 
    </shape>   
</item>
</selector>

両方の単色を変更する必要があり、それらにアクセスする方法がわかりません。バックグラウンドのイメージボタンにアクセスしようとしましたが、何も起こりません。

4

3 に答える 3

0

あなたが望むものを達成するために私が知っている限り、実行時にシェイプドローアブルを作成する必要があり、次に状態リストを作成して background として設定できます。このコードを使用して、実行時にシェイプ ドローアブルを作成できます

ShapeDrawable footerBackground = new ShapeDrawable();

// The corners are ordered top-left, top-right, bottom-right,
// bottom-left. For each corner, the array contains 2 values, [X_radius,
// Y_radius]
float[] radii = new float[8];
radii[0] = activity.getResources().getDimension(R.dimen.footer_corners);
radii[1] = activity.getResources().getDimension(R.dimen.footer_corners);

radii[2] = activity.getResources().getDimension(R.dimen.footer_corners);
radii[3] = activity.getResources().getDimension(R.dimen.footer_corners);

footerBackground.setShape(new RoundRectShape(radii, null, null));

int color = ((Application) activity.getApplication()).getColor();

footerBackground.getPaint().setColor(color);

views.setBackgroundDrawable(footerBackground);

この部分は、この投稿Android: Change Shape Color in runtime で受け入れられた回答から取得したものです。

実行時のカラー状態リストの作成に関するドキュメントを参照してくださいhttp://developer.android.com/reference/android/content/res/ColorStateList.html

于 2013-10-04T11:46:55.170 に答える