【问题标题】:Android - Subclass View, Set Class VariableAndroid - 子类视图,设置类变量
【发布时间】:2012-02-23 21:00:32
【问题描述】:

我创建了一个简单的视图。我希望能够通过 Eclipse GUI 插件的设计工具设置它的类变量。或者通过 .xml。这可能吗?

这是我目前拥有的:

package com.lifecoderdev.android.drawing1;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class BoundedView extends View
{
  public String CellName = "No name.";

  public BoundedView( Context context )
  {
    super( context );
  }

  public BoundedView( Context context, AttributeSet attrs )
  {
    super( context, attrs );
  }

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


  @Override
  public void onDraw(Canvas canvas)
  {
  }

  @Override
  public boolean onTouchEvent( MotionEvent event )
  {
    Toast t = Toast.makeText( getContext(), "Selected: " + CellName + "!", Toast.LENGTH_SHORT );
    t.show();

    return false;
  }

}

视图的唯一工作是放置在相对布局中,当它被点击时,发出警告消息。这部分工作得很好,但我真的希望能够通过 XML 或属性面板设置 CellName。

【问题讨论】:

    标签: android xml view


    【解决方案1】:

    在以下链接中阅读casebash 的答案:

    Android Custom View Properties

    【讨论】:

      【解决方案2】:

      您必须使用TypedArray 从自定义View(public BoundedView( Context context, AttributeSet attrs, int defStyle )) 的最后一个构造函数中的xml 布局中获取属性。

      Android SDK 中的Snake 游戏就是一个很好的例子。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        相关资源
        最近更新 更多