Android 将XML与组件视图链接
我目前正在努力解决我认为应该很简单的问题。 我在一个XML文件中创建了一个LinearLayout,我想链接到一个扩展了LinearLayout的Android 将XML与组件视图链接,android,xml,android-layout,Android,Xml,Android Layout,我目前正在努力解决我认为应该很简单的问题。 我在一个XML文件中创建了一个LinearLayout,我想链接到一个扩展了LinearLayout的CustomComponent,这意味着我是向后开始的 通常,我首先创建一个CustomComponent,然后创建一个由标记链接的XML文件,(如果我没有弄错的话,这是链接它们的唯一方式(?)),我在onDraw()中做一些事情。但是在这个项目中,我通过XML而不是onDraw()进行布局 通过setContentView(R.layout.cust
CustomComponent
,这意味着我是向后开始的
通常,我首先创建一个CustomComponent,然后创建一个由标记链接的XML文件,
(如果我没有弄错的话,这是链接它们的唯一方式(?)),我在onDraw()中做一些事情。但是在这个项目中,我通过XML而不是onDraw()
进行布局
通过setContentView(R.layout.customView)
将XML与活动链接起来,但我不能在CustomComponent
中真正做到这一点,因为我没有继承onCreate()
方法
旁注:在XML中,所有my ImageButton都有android:onClick=chooseButton
,但由于明显的原因,它找不到这个方法
关于这个问题有什么想法吗
编辑:
这两个文件似乎没有链接,因为在xmlandroid:onClick=“chooseButton”
IDE中说:“无法解析符号chooseButton”
XML:
现在还不清楚你想做什么,但是如果你想把这些图像按钮添加到你的自定义视图,那么你可以把它们放在
标签中,就像你现在用的
一样。然后,您可以通过几种不同的方式访问它们findViewById()
在onfinishflate()
中,覆盖addView(View,int,LayoutParams)
以在添加时获取它们,等等。您还可以在每个上设置OnClickListener
以调用您的chooseButton()
方法。对于不清楚,我深表歉意。这两个文件似乎没有链接,因为在XMLandroid:onClick=“chooseButton”中,IDE说:“无法解析符号chooseButton”,您将删除这些属性。您之所以会出现此错误,大概是因为该方法不存在于DialView
中。不能使用该属性将OnClickListener
目标设置为活动以外的任何对象。您需要在每个ImageButton
上设置OnClickListener
,您自己在代码中。
<?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-----/>
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){}
}