如何自定义按钮(有两个文本域)在Android [英] how to Custom Button (has two TextFields) on Android

查看:103
本文介绍了如何自定义按钮(有两个文本域)在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开发一个按钮,有两个标签贴在

I need to develop a button that has two label in.

我找到自定义视图的一些好文章,但我无法想象我如何可以创建一个为myButton类(在它的自定义布局)扩展按钮......是有可能..

I find some good articles about custom views, but I can't imagine that how can I create a myButton Class(with custom layout in it) extends button... is it possible..

另外,在XML的一些看法,布局... 我们怎样才能做到这一点?

Also in XML some views,layouts... How can we do this??

推荐答案

我所著这个像,..我有一个布局的问题。我不能填满屏幕上有两个按钮。 parentlayout填满屏幕,但我不能把应该..这两个按钮

I writed this like,.. I have a layout problem. I cant fill screen with two buttons. parentlayout fills screen, but I cant these two buttons put should be..

我的按键布局:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:gravity="center">
        <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/xbutton2_icon" />
        <TextView
                android:id="@+id/xbutton2_tv"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                 />
</LinearLayout>

和它的类:

public XButton2(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.xbutton2, this);
        icon = (ImageView) view.findViewById(R.id.xbutton2_icon);
        tv = (TextView) view.findViewById(R.id.xbutton2_tv);
        init(attrs);
    }
    protected void init(AttributeSet attrs) {
        parseAttributes(attrs);
        setAttrs();
    }
    protected void parseAttributes(AttributeSet attrs) {
        TypedArray param = getContext().obtainStyledAttributes(attrs,
                R.styleable.com_matriksdata_bavul_XButton2);
        this.text = param
                .getString(R.styleable.com_matriksdata_bavul_XButton2_text);
        String str = param
                .getString(R.styleable.com_matriksdata_bavul_XButton2_icon);

        if (str != null) {
            String[] arr = str.split("\\/");
            this.iconResorucesID = getResources().getIdentifier(
                    getContext().getApplicationContext().getPackageName() + ":"
                            + arr[arr.length - 2] + "/"
                            + arr[arr.length - 1].split("\\.")[0], null, null);
        }
        this.textSize = param.getFloat(
                R.styleable.com_matriksdata_bavul_XButton2_textSize, 40);

        param.recycle();
    }

    protected void setAttrs() {
        if (text != null) {
            tv.setText(text);
            tv.setTextSize(XUtil.convertToPixcell(getContext(), textSize));
            // tv.setTextColor(textColor);
            // tv.setHighlightColor(textSelectedColor);
        }
        if (iconResorucesID != 0)
            icon.setImageResource(iconResorucesID);

    }

    public void setChecked(boolean isChecked) {
        if (isChecked) {
            // setBackgroundResource(selectedBg);
            tv.setSelected(true);
        } else {
            tv.setSelected(false);
            // setBackgroundResource(bg);
        }
        this.isChecked = isChecked;
    }

和它是我用它。

<com.matriksdata.widget.SplitButtonController
                            android:layout_marginLeft="8dip"
                            android:layout_marginRight="8dip"
                            android:layout_width="fill_parent"
                            android:orientation="horizontal"
                            android:layout_height="wrap_content"
                            android:gravity="center_vertical"

                            >
                            <com.matriksdata.widget.XButton2
                                    mtx:text="@string/strFlight"
                                    mtx:textSize="20"
                                    mtx:icon="@drawable/flight_buttonicon"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content" 
                                    android:layout_weight="1"/>

                            <com.matriksdata.widget.XButton2
                                    mtx:text="@string/strBus"
                                    mtx:textSize="20"
                                    mtx:icon="@drawable/bus_buttonicon_gray"
                                    android:layout_height="wrap_content"
                                    android:layout_width="fill_parent"
                                    android:layout_weight="1" />
                    </com.matriksdata.widget.SplitButtonController>

这篇关于如何自定义按钮(有两个文本域)在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆