开发者

android custom button

开发者 https://www.devze.com 2023-01-08 05:50 出处:网络
i would like to create a custom button like this: http://i77.photobucket.com/albums/j74/bertyhell/button.png?t=1279626910

i would like to create a custom button like this:

http://i77.photobucket.com/albums/j74/bertyhell/button.png?t=1279626910

but that doesn't work i need to show the text on the buttons with absolute positioning relative to the buttons origin all strings need to be changeable at runtime

but the button below can be a regular button

how can i do this?

if i create a button by inheriting from android.widget.button i have to draw the whole button myself?

is there an easier way?

EDIT

ok i tried the inherit from relative layout

like this:

    package be.smarttelecom.datacheck.views;

    import android.content.Context;
    import android.util.Log;
    import android.widget.RelativeLayout;
    import android.widget.TextView;

    public class AlertButton extends RelativeLayout {

        private TextView roomNumber;

        public AlertButton(Context context, String roomNumber) {
            super(context);
            try{
            this.alertId = alertId;
            // TODO Auto-generated constructor stub
            this.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            TextView room = new TextView(context);
            room.setFocusable(false);
            room.setText(R.string.room);
            room.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

            this.roomNumber = new TextView(context);
            this.roomNumber.setFocusable(false);
            this.roomNumber.setId(R.id.roomNumber);
            this.roomNumber.setTextSize(26);
            this.roomNumber.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            this.roomNumber.setText(roomNumber);

            }catch(Exception ex){
                Log.i("test", "problem during construction of alert button: " + ex.getMessage());
            }
        }

        public void setRoomNumber(String number){
            roomNumber.setText(number);
        }

        public void setDescription(String description){
            this.description.setText(description);
        }
    }

when i add it like this:

        TableRow row2 = new TableRow(this);
        AlertButton alert = new AlertButton(this, "1","14","wc","krist");
        row2.addView(alert);
        container.addView(row2, 0);

i get an empty pa开发者_JAVA百科ge and no errors at all

any idea what i did wrong?

how do i put the textfields on certain positions? put them inside an absolute layout and add x an y coordinates? (absolute layout is depricated, alternative?)


I think the easiest way is to create a custom view that extends RelativeLayout. You can then add 4 TextViews in whatever arrangement you wish with whatever customization you need. To make it look and act like a button, you can add a button to your layout with the width and height set to fill_parent. Make sure that your TextViews are not focusable and that the button is sent to the back of the layout.

Alternatively you can set the RelativeLayout's properties in your custom view to be clickable=true and focusable=true. Then add a drawable as your view background that looks like a button. Use a selector to handle all the focused/pressed states like a real button does. Details on how to do that can be found here: Standard Android Button with a different color

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号