将XML与组件视图链接

时间:2018-04-27 09:41:20

标签: android xml android-layout

我目前正在努力应对我认为应该非常简单的事情。 我在XML文件中创建了一个LinearLayout,我希望链接到CustomComponent LinearLayout,这意味着我开始倒退了。

通常我首先创建一个CustomComponent,这会创建一个由标签<my.package.CustomComponent>链接的XML文件(如果我没有弄错,这是他们链接的唯一方式(?)),我这样做onDraw()中的内容。但是在这个项目中,我通过XML进行布局而不是onDraw()

将XML与活动相关联是由setContentView(R.layout.customView)完成的,但我无法在CustomComponent中执行此操作,因为我没有继承onCreate()方法。< / p>

旁注:在XML中,我的所有图像按钮都有android:onClick=chooseButton,但由于显而易见的原因,它无法找到此方法......

关于这个问题的任何想法?

修改: 这两个文件似乎没有链接,因为在xml android:onClick="chooseButton"中IDE说:&#34;无法解析符号chooseButton&#34;

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dial_view_id"
    android:layout_width="match_parent"
    tools:context=".DialView"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <com.package.CustomView
        android:id="@+id/drawingview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1">


        <ImageButton
            android:id="@+id/button1"
            android:onClick="chooseButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            android:background="@drawable/ic_dialpad_1_blue" />

        <ImageButton
            android:id="@+id/button2"
            android:onClick="chooseButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/ic_dialpad_2_blue" 
                 ------code repeated below-----/>

CustomView:

public class CustomView extends LinearLayout {
    private String mExampleString;
    private int mExampleColor = Color.RED;
    private float mExampleDimension = 0;
    private Drawable mExampleDrawable;
    private TextPaint mTextPaint;
    private float mTextWidth;
    private float mTextHeight;
    private ImageButton button_0, button_1, button_2, button_3, button_4, button_5, button_6;
    private ImageButton button_7, button_8, button_9, button_star, button_pound;
    private boolean clicked = false;
    private SparseIntArray drawables = new SparseIntArray();

    public DialView(Context context) {
        super(context);

    }

    public DialView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public DialView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {
        // Load attributes
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.DialView, defStyle, 0);

        mExampleString = a.getString(
                R.styleable.DialView_exampleString);
        mExampleColor = a.getColor(
                R.styleable.DialView_exampleColor,
                mExampleColor);
        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
        // values that should fall on pixel boundaries.
        mExampleDimension = a.getDimension(
                R.styleable.DialView_exampleDimension,
                mExampleDimension);

        if (a.hasValue(R.styleable.DialView_exampleDrawable)) {
            mExampleDrawable = a.getDrawable(
                    R.styleable.DialView_exampleDrawable);
            mExampleDrawable.setCallback(this);
        }

        a.recycle();

        // Set up a default TextPaint object
        mTextPaint = new TextPaint();
        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextAlign(Paint.Align.LEFT);

        // Update TextPaint and text measurements from attributes
        invalidateTextPaintAndMeasurements();
    }

    private void invalidateTextPaintAndMeasurements() {
        mTextPaint.setTextSize(mExampleDimension);
        mTextPaint.setColor(mExampleColor);
        mTextWidth = mTextPaint.measureText(mExampleString);

        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = fontMetrics.bottom;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    public void getButtons(){}

    public void chooseButton(View v){}

    public void switchBackground(ImageButton button){}
}

0 个答案:

没有答案