2010-12-22 56 views
0

嘿所有, 我已經創建了一個使用Jpanels內的Jpanels的Java程序。這方面的一個例子是具有包含按鈕的側視圖,按鈕可以在按下時更改主視圖。主視圖也有可用於改變自身的按鈕。佈局中的Android佈局,用不同的類來處理它們的輸入?

我想重新在android上創建這個。我從xml設置了我的佈局,並帶有包含按鈕和主視圖的側面板。

還有一個xml包含我想要的主視圖。

現在我正在努力獲得這個單獨的XML在主視圖中顯示,並不能弄清楚我將如何讓類來處理自己的輸入。我不想將代碼粘貼到main.xml中,因爲我想保持面向對象的東西,並且每個視圖都需要一些自定義元素,例如不同的文本視圖。

的什麼我試圖爲Android轉換下面是一個例子: -

//The ViewInt class has its own layout 
ViewInt tempView = new ViewInt(i, mFloorNo); 
//The viewInt class is then added to the main window. 
mainWindowPanel.add(tempView); 

該小代碼位將在主創建一個視圖,將其添加到主視圖,然後任意輸入窗口由該類處理,但同時,不在此主窗口中的任何輸入都由另一個類(可能是根類)處理。 任何幫助非常感謝,謝謝。

澄清: 該程序有一個側面板佈局,包含按鈕。還有一個空的主視圖佈局。我正在嘗試爲這個主視圖佈局設置一個.xml佈局,並且讓它能夠在該主視圖中按下任何按鈕時,該佈局的類將處理它。

+0

我不完全理解你的問題,你想要做的是與自定義內容嵌套的意見?你還想在每個視圖上單獨處理設備輸入事件?我真的不明白你的問題,因爲只有一個視圖有時間焦點... – Franco 2010-12-22 22:06:57

回答

0

如果我明白你要做什麼,那麼我會擴展其中一個佈局類來創建我的面板類,然後將它們嵌套在main.xml佈局文件中的另一個佈局(RelativeLayout)中。

public class MyPanel extends LinearLayout 
    implements Button.onClickListener { 
    // Do things which are common to the side & main panels in this class 
} 

public class MySidePanel extends MyPanel { 
} 

public class MyMainPanel extends MyPanel { 
} 

然後在res /佈局/ main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <com.mydomain.MySidePanel 
     android:orientation="vertical" 
     android:id="@+id/side_panel_layout" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" > 
     <Button 
      android:id="@+id/sp_Button1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <!-- Do other button --> 
    </com.mydomain.MySidePanel> 
    <com.mydomain.MyMainPanel> 
    <!-- main panel layout here --> 
    </com.mydomain.MyMainPanel 
</RelativeLayout> 

在上面,我明明只把在側面板一個按鈕,左側的主面板空,但你應該能夠從中看到我的方法。您還需要添加相對佈局android:layout_ properties。

在您的活動中,只需setContentView(...)並創建面板類的實例。

編輯:我的意思是說,雖然上面似乎絕對設定的總體佈局,沒有什麼在運行時創建新MyMainPanel佈局,並在RelativeLayout的父母交換他們代替原來的阻止你。