Android 如何将图像添加到自定义按钮
我有非常简单的问题,如何在自定义按钮中添加图像Android 如何将图像添加到自定义按钮,android,button,imagebutton,Android,Button,Imagebutton,我有非常简单的问题,如何在自定义按钮中添加图像 <com.example.KeyButton android:id="@+id/custombutton" android:layout_gravity="center_vertical" android:layout_height="fill_parent" /> 并将“您的图片”放入可绘制文件夹。添加此标签an
<com.example.KeyButton
android:id="@+id/custombutton"
android:layout_gravity="center_vertical"
android:layout_height="fill_parent" />
并将“您的图片”放入可绘制文件夹。添加此标签
android:background=“@drawable/your\u image”
必须添加才能添加图像
<com.example.KeyButton
android:id="@+id/custombutton"
android:layout_gravity="center_vertical"
android:layout_height="fill_parent"
android:background"@drawable/image" /> //here will be your image.
//这是您的图像。
将按钮的图像放在drawble文件夹中,并将此代码放在xml文件中
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/btn_your_btn_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/set_alarm_button" />
</LinearLayout>
您可以通过编程方式使用以下方法:
setBackground(Color c); and
setBackground(Drawable d);
setBackgroundResource(int resId);
否则,您可以使用如上所述的xml方式使用android:background
与在ImageView
`android:background=“@drawable/image”上添加图像的方式相同。谢谢您的回复,但我喜欢类似视图的图像按钮有png图像。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<Button
android:id="@+id/btn_your_btn_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/set_alarm_button" />
</LinearLayout>
setBackground(Color c); and
setBackground(Drawable d);
setBackgroundResource(int resId);