我有一个ImageButton,按下按钮时应该更新。 onClick事件被触发,我认为图像本身会更新(因为BitmapDrawable @id会发生变化)。
这是我的代码
public void gotoBasicControls(View view) {
viewPager.setCurrentItem(0, true);
bottomButtonRemote.setImageResource(R.drawable.remote_selected_icon_transparent);
bottomButtonRemoteText.setTextColor(getResources().getColor(R.color.colorAppOrange));
}
这是我的xml
<TableLayout
android:id="@+id/bottomBar"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:stretchColumns="*"
android:background="@color/colorWhite"
android:gravity="bottom"
android:paddingBottom="3dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<ImageButton
android:maxHeight="35dp"
android:id="@+id/bottomRemote"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="@null"
android:clickable="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:onClick="gotoBasicControls"
android:src="@drawable/remote_icon_transparent"/>
<ImageButton
android:maxHeight="20dp"
android:id="@+id/bottomScoreboard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="@null"
android:clickable="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:onClick="gotoScoreboard"
android:src="@drawable/scoreboard_icon_transparent" />
<ImageButton
android:maxHeight="35dp"
android:id="@+id/bottomStopwatch"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:background="@null"
android:clickable="true"
android:paddingLeft="13dp"
android:paddingRight="15dp"
android:onClick="gotoStopwatch"
android:src="@drawable/stopwatch_icon_transparent" />
<ImageButton
android:maxHeight="35dp"
android:id="@+id/bottomSettings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:background="@null"
android:clickable="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:onClick="gotoSettings"
android:src="@drawable/settings_icon_transparent" />
</TableRow>
<TableRow>
<TextView android:text="Basic"
android:id="@+id/bottomRemoteText"
android:textAlignment="center"/>
<TextView android:text="Scoreboard"
android:id="@+id/bottomScoreboardText"
android:textAlignment="center"/>
<TextView android:text="Stopwatch"
android:id="@+id/bottomStopwatchText"
android:textAlignment="center"/>
<TextView android:text="Settings"
android:id="@+id/bottomSettingsText"
android:textAlignment="center"/>
</TableRow>
我还应该注意,setTextColor也不起作用。
我尝试过的事情是:
使用URI而不是drawable
使按钮及其所在的布局无效。
答案 0 :(得分:3)
public void gotoBasicControls(View view) {
viewPager.setCurrentItem(0, true);
((ImageButton)view).setImageResource(R.drawable.remote_selected_icon_transparent);
buttonText.setTextColor(getResources().getColor(R.color.colorAppOrange));
}