使用 Graphics 在 Android 中构建自定义界面

发布于 2024-12-16 13:19:17 字数 150 浏览 4 评论 0原文

我希望你能引导我找到好的链接,或者给我一些我必须学习和阅读的基本元素,以便能够在 android 中构建自定义用户界面。 我所说的自定义是指界面将包含图像按钮,而不是常规的 Android 按钮。 此外,我还必须根据用户操作动态生成自定义按钮,并且这些生成的按钮应该具有与其关联的事件。

I want you to direct me to good links or give me the basic elements that I have to study and read about to be able to build a custom user interface in android.
What I mean by custom is that the interface will contain buttons that are images not a regular android button.
Also I have to generate custom buttons on the fly based on user action and those generated buttons should have events associated with them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-12-23 13:19:17

有关按钮的通用信息此处

要使用按钮的图像,您需要一个 < a href="http://developer.android.com/reference/android/widget/ImageButton.html" rel="nofollow">android.widget.ImageButton。可绘制选择器的示例(将其放在 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>

因此您可以针对不同的状态使用不同的图像。
要动态生成按钮,您可以使用任何布局管理器(FrameLayout、RelativeLayout、LinearLayout)定义基本布局,在 Activity 中通过 id (findViewById()) 找到它并使其可见:

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
}

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文