Android ImageButton按钮状态图像未显示



我在Wallace Jackson的"Android应用程序的绝对初学者",第三版,

目前在第9章末尾(第350页)。

我已经更新了资源目录中的PNG文件、imagebutton状态文件和主活动布局。我得到了一些意想不到的行为,这并不奇怪,因为大多数书的代码示例都不是开箱即用的,但我对此有点困惑。图像按钮不会显示在滑动窗格布局的左侧窗格中。我可以在图像应该在的地方看到三个白色的小矩形,但它们应该是正方形(嗯,圆形,但从技术上讲,图像仍然是透明的正方形)。里面也应该有一些星系的照片。

安卓工作室没有给我任何错误,状态文件甚至有缩略图显示适当的按钮状态资源。

我使用的是AndroidStudio 1.1.0和JRE 1.7.0_71。我正在使用Android 5.1 在谷歌Nexus 7上调试

按钮状态文件(注意stackoverflow正在获取终止选择器标签)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_hovered="true" android:drawable="@drawable/imagebutton1hover" />
<item android:state_pressed="true" android:drawable="@drawable/imagebutton1press" />
<item android:state_focused="true" android:drawable="@drawable/imagebutton1focus" />
<item android:drawable="@drawable/imagebutton1normal" />

布局文件

<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/spLayout"
android:background="@drawable/ninepatchframe">
<LinearLayout
android:layout_width="110dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/galaxyOne"
android:src="@drawable/ibstates1"
android:contentDescription="@string/galaxyone"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/galaxyTwo"
android:src="@drawable/ibstates2"
android:contentDescription="@string/galaxytwo"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/galaxyThree"
android:src="@drawable/ibstates3"
android:contentDescription="@string/galaxythree"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/galaxyinfoscreen">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#00000000"
android:layout_margin="12dp">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="48dp" android:textStyle="bold"/>
<TextView
android:text="@string/galaxy_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF" />
<TextView
android:text="@string/galaxy_solar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/galaxy_habit"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/galaxy_colony"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/galaxy_pop"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/galaxy_fleet"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/galaxy_ships"
android:textColor="#FFFFFF" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#00000000"
android:layout_marginTop="33dp">
<TextView
android:text="@string/name_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:id="@+id/name"/>
<TextView
android:text="@string/solar_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:id="@+id/solar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/habit_data"
android:textColor="#FFFFFF"
android:id="@+id/habit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/colony_data"
android:textColor="#FFFFFF"
android:id="@+id/colony"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pop_data"
android:textColor="#FFFFFF"
android:id="@+id/pop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fleet_data"
android:textColor="#FFFFFF"
android:id="@+id/fleet"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ships_data"
android:textColor="#FFFFFF"
android:id="@+id/ships"/>
</LinearLayout>
</LinearLayout>

另一件奇怪的事。。。不认为这有关联,但谁知道呢。书中的屏幕截图显示了整个滑动窗格面板周围的九个接线板,但在我的应用程序中,九个接线片完全被右侧面板覆盖(根本看不到它的一侧)。

有什么想法吗?

其他人已经发布了答案。不幸的是,链接帖子的OP从未承认PSIXO的正确答案。

安卓工作室中带有选择器的图像按钮错误渲染

对于那些不想阅读整个线程的人来说,物理文件名必须以".xml"结尾。关于Android编程的书指示读者创建一个名为"ibstates"的文件,但名称必须是"ibstates.xml",否则Android Studio将在将其作为资源引用时中断并退出。

最新更新