我希望你能指导我找到好的链接,或者给我一些我必须学习和阅读的基本元素,以便能够在 android 中构建自定义用户界面。我所说的自定义是指界面将包含图像按钮,而不是常规的 android 按钮。此外,我必须根据用户操作动态生成自定义按钮,并且这些生成的按钮应该具有与之关联的事件。
问问题
236 次
1 回答
1
Generic info about buttons here
To use image for a button you need an android.widget.ImageButton. Example of drawable selector (put it in res/drawable/):
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
So you can use different images for various states. To generate buttons on the fly you can define base layout with any layout manager (FrameLayout, RelativeLayout, LinearLayout), find it by id (findViewById()) within your Activity and make it visible:
public void createContainerForImageView() {
LinearLayout container = new LinearLayout(this);
container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
container.setOrientation(LinearLayout.HORIZONTAL);
ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);
container.addView(img); // or you can set the visibility of the img
}
Hope this helps.
于 2011-11-17T15:53:03.467 回答