2013-04-02 40 views
0

我刚刚了解android开发,并且遇到了一些使其工作的问题。在android中设置自定义视图为布局

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

viewer.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)的实例后,我不确定如何将此对象设置到我为视图提供的空间中。

这是我的活动类:

Viewer.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命令,认为占据了整个页面,覆盖了在按钮底部。任何人都可以对此有所了解吗?我觉得我很接近,但我一直在玩它一段时间,而我似乎卡住了。

回答

1

检查执行自定义视图部分here。你需要重写onLayout和onMeasure,这样你可以告诉你的容器有多大。

0

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

相关问题