【问题标题】:Rotating a RelativeLayout in Android 2.3在 Android 2.3 中旋转 RelativeLayout
【发布时间】:2013-09-02 13:29:31
【问题描述】:

我是 android 新手,在为我正在开发的两人游戏旋转相对布局方面需要一些帮助。我看过here 的帖子,并查看了关于 SO 上同一主题的许多其他帖子。我的第一个问题是如何调用我刚刚创建的新课程?对于所有不只是告诉我使用 android:rotation (在 2.3- 中不可用)的帖子,我创建了一个新类,但我收到一条通知说该类从未被调用。我调用类类似于我调用方法的方式吗?还是AndroidManifest中有一些命令?最后,新类如何只旋转一个相对布局而不旋转另一个?我只是想知道它应该如何工作。
这是我应该制作的新课程:

public class MyRelativeLayout extends RelativeLayout {    
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

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

public MyRelativeLayout(Context context) {
    super(context);
    init();
}

private void init() {
    setStaticTransformationsEnabled(true);
}

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
    t.setTransformationType(Transformation.TYPE_MATRIX);
    Matrix m = t.getMatrix();
    m.reset();
    m.postRotate(180, child.getWidth() / 2.0f, child.getHeight() / 2.0f);
    return true;

感谢您的帮助

【问题讨论】:

标签: java android class android-studio


【解决方案1】:

在你的活动的 oncreate() 方法中做setContentView(new MyRelativeLayout(this))

【讨论】:

  • 这是 Ritaban 的一种方法,是的。不要忘记new MyRelativeLayout() 之后的() 令牌。但是,在 许多 情况下,OP 可能希望以其他方式将其自定义视图添加到视图层次结构中。也许我们应该等到 OP 表示更多?
  • @Tom 在 'new MyRelativeLayout' 之后应该在 '()' 中添加什么?
  • 新建 MyRelativeLayout(this)
猜你喜欢
  • 2014-02-18
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
  • 2010-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多