0

我只是在学习 android 开发,我在让它工作时遇到了一些问题。

我有一个使用相对布局的活动。我需要它在底部有 2 个按钮,然后在底部的正上方,我希望我的自定义视图占据其余空间。

查看器.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/viewerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<sketchViewer.AnimationPanelView
    android:id="@+id/animationView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/homeFromViewerButton"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/homeFromViewerButton"
    android:layout_width="640dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Replay" />

<Button
    android:id="@+id/replayButton"
    android:layout_width="640dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Home" />

</RelativeLayout>

我遇到的问题是,当我运行我的程序时,我需要将一些参数传递给我的自定义视图构造函数,以便我的自定义视图决定它应该绘制什么。因此,在创建自定义视图 (AnimationPanelView) 的实例后,我不确定如何将此对象设置到为视图提供的空间中。

这是我的活动课:

查看器.java

public class Viewer extends Activity {

AnimationPanelView animationPanelView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewer);   

    animationPanelView = new AnimationPanelView(this, true /*, more parameters here */);
    animationPanelView.setBackgroundColor(Color.GREEN);
    RelativeLayout v = (RelativeLayout) findViewById(R.id.viewerLayout);
    v.addView(animationPanelView);      
}

现在,使用我的 v.addView 命令,视图占据了整个页面,覆盖了底部的按钮。任何人都可以对此有所了解吗?我觉得我很接近,但我已经玩了一段时间了,我似乎被困住了。

4

2 回答 2

1

在此处查看实施自定义视图部分。您需要覆盖 onLayout 和 onMeasure 以便告诉您的容器有多大。

于 2013-04-02T05:40:49.827 回答
0

您正在向布局添加另一个自定义视图,而不是应该使用 animationPanelView = (AnimationPanelView) findViewById(R.id.animationView); animationPanelView.setBackgroundColor(Color.GREEN);

于 2013-04-02T05:47:54.953 回答