2011-09-04 121 views
0

我想創建一個窗口,頂部有一個自定義視圖,底部有三個按鈕。我希望視圖能夠佔用按鈕創建後留下的房地產。我的佈局XML是如下自定義視圖的Android佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" > 
    <FrameLayout 
    android:id="@+id/CustomFrame" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#ffffff" > 
    <Button 
     android:id="@+id/Button01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/ok" 
     android:layout_marginBottom="10dip" 
     android:layout_marginTop="10dip" /> 
    <Button 
     android:id="@+id/Button02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/cancel" 
     android:layout_marginBottom="10dip" 
     android:layout_marginTop="10dip" /> 
    <Button 
     android:id="@+id/Button03" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/clear" 
     android:layout_marginBottom="10dip" 
     android:layout_marginTop="10dip" /> 
    </LinearLayout> 
</LinearLayout> 

我再加入我的自定義視圖的窗口,並在onCreate方法的代碼

setContentView(R.layout.drawitwindow); 
    FrameLayout layout = (FrameLayout)findViewById(R.id.CustomFrame); 
    mView = new MyCustomView(this); 
    layout.addView(mView); 

但是,自定義視圖佔據整個屏幕!如果我刪除將自定義視圖添加到框架的代碼,則按鈕欄會出現在窗口的頂部(如我所期望的那樣)。

我試圖重寫onMeasure方法,加入

 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
    this.setMeasuredDimension(parentWidth,parentHeight); 

但是這並沒有什麼差別。

我試過佈局寬度和高度的各種組合,並且似乎沒有任何區別。

所有的幫助將非常感激地接受......

回答

0

更改的FrameLayout安卓layout_height爲wrap_content。

還添加到FrameLayout和按鈕的線性佈局android:layout_weight =「1.0」。

我希望這會有所幫助。

+0

芮,感謝您的提示。這樣做會讓事情變得更好,但並不完全解決問題 - 按鈕現在出現,但它們具有不正確的(小)高度,因此文本不顯示。 我瀏覽了幾個附錄,發現了一個可行的解決方案。不幸的是,由於我的空間不足,我無法發佈新的XML!基本上,我在頂層使用了一個相對佈局,正如問題「如何在android中的視圖中建立自定義視圖」 我不知道爲什麼我之前沒有找到這個主題;在問我的問題之前,我花了幾個小時尋找解決方案。 –

+0

是的,相對佈局更易於使用。如果您在每個佈局上都使用了layout_weight,它就會起作用。 –

+0

謝謝,瑞 - 我會回到線性佈局並使用layout_weight來玩。這一切都有助於理解。 –