2013-04-02 39 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);

相關問題