如何添加图像按钮
问题描述:
我会在我的应用程序使用图像按钮,按钮标题,我需要显示在我使用这个代码的按钮,按钮标题,但它并没有显示字幕如何添加图像按钮
<ImageButton
android:id="@+id/try_button"
android:background="@drawable/custom_button"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="10dp"
android:text="@string/trybutton_caption" />
我怎样才能做到这一点?
谢谢。
答
使用Button
而不是ImageButton
。
有使用:
android:drawableTop="@drawable/image"
android:text="@string/text"
仅供参考使用drawableTop
,可绘制将文本上面绘制。
答
您需要创建一个包含图像视图和文本视图的布局。然后,您可以使用父布局的onClick事件将其视为一个按钮。这里有一个例子:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/image_button_background"
android:gravity="center"
android:onClick="doCustomers"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/ic_menu_box" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="10dp"
android:text="Customers" />
</LinearLayout>
答
另一种选择可能是创建一个TextView,并设置compound drawable top到你的形象:
android:drawableTop="@drawable/custom_button"
或通过代码:
textView.setCompoundDrawableWithIntrinsicBounds(0, R.drawable.custom_button, 0, 0)
答
最后,我这样做,你的帮助:
<Button
android:id="@+id/try_button"
android:background="@drawable/custom_button"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="10dp"
android:text="@string/trybutton_caption"/>
谢谢朋友, – Tikitaka 2013-04-28 14:50:08
欢迎您:) – stinepike 2013-04-28 15:06:29
@Tikitaka看起来好像你谢过我,,接受我的答案,然后接受另一个答案...反正..运气好的 – stinepike 2013-04-28 15:10:42