2013-03-15 58 views
1

我有一個需求,其中有2個編程生成的屏幕和2個xml佈局。現在我需要在飛行中結合這些佈局多次。以編程方式結合幾種類型的xml佈局android

對於離,我有屏幕1 - 編程創建的,屏幕2 - 編程創建屏幕3-從XML佈局,屏幕4 - 從XML佈局

我的最終佈局設計應該是一個單一的屏幕屏幕1,屏幕2,屏幕3,屏幕4,屏幕2 ...,所有屏幕根據我輸入的屏幕數量共享相同的屏幕空間。請讓我知道這個方法。一些屏幕有相對佈局和一些線性佈局。所以它應該結合這些。

回答

3

您需要在主佈局上調用addView()。構建主佈局(其中包含所有其他佈局)後,addView()方法將爲現有主佈局添加新視圖。

要添加新佈局,您需要首先對其進行充氣。

LinearLayout primaryLayout; 

LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
LinearLayout newLayout = (LinearLayout)layoutInflater.inflate(R.layout.your_new_layout, null, false); 

primaryLayout.addView(newLayout); 

AddView還提供了一個索引選項,用於將新佈局放置在主佈局中的特定點上。

開始嘗試用空白,XML佈局(比如叫primary_layout):

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


</RelativeLayout> 

然後,當你的活動開始,設置,再膨脹,並添加按照需要:

setContentView(R.layout.primary_layout); 
LinearLayout primaryLayout = (LinearLayout) findViewById(R.id.primaryLayout); 

然後,您可以將新視圖添加到該視圖。至於多次添加,我相信這是通過引用完成的,所以它只能看到一個視圖。嘗試在方法中構建視圖,並返回視圖。如:

private View buildNewView(){ 

    LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    LinearLayout newView = (LinearLayout)layoutInflater.inflate(R.layout.my_new_view null, false); 


    return newView ; 
} 

並通過primaryLayout.addView(buildNewView();調用它。

+0

感謝您的回覆,我照你說的去做。 RelativeLayout primaryLayout = new RelativeLayout(this); \t \t LayoutInflater layoutInflater =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); \t \t RelativeLayout newLayout =(RelativeLayout)layoutInflater.inflate(R.layout.layout3,null,false); \t \t RelativeLayout newLayout1 =(RelativeLayout)layoutInflater.inflate(R.layout.layout4,null,false); \t \t primaryLayout.addView(newLayout); \t \t primaryLayout.addView(newLayout1); \t \t setContentView(primaryLayout); 但是,只有最後一個視圖被添加,你能指導y嗎? – bharath 2013-03-15 16:14:59

+0

此外,我不能多次添加相同的視圖,它給出了一個錯誤,指出指定的孩子已經有一個父母,第一次調用removeview – bharath 2013-03-15 16:27:59

+0

上面編輯! – 2013-03-15 18:42:47

0

你可以看看碎片。他們似乎正是你所需要的。這裏是他們的TrainingAPI Guides的鏈接。

在您的xml文件中,您可以在父母的LinearLayout父級中指定4個子佈局,每個子佈局的屬性爲android:layout_weight="1",因此每個子佈局只佔用相同的空間量。建議設置爲android:layout_width="match_parentandroid:layout_height="0dp"現在,您可以將每個子佈局的ID標記爲id1,id2,id3等,但是您也可以將兩個佈局標記爲android:id="@+id/fragment_container_firstandroid:id="@+id/fragment_container_second

在Java代碼中,您可以將contentView設置爲xml文件的標識(setContentView(R.layout.myXMLLayout);),通過遵循上面提供的培訓指南鏈接創建兩個Fragment實例,並將這些視圖添加到您之前設置的容器中您的xml文件使用類似getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container_first, firstFragment).commit();getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container_second, secondFragment).commit();(如果您使用支持庫,這是培訓指南使用的內容)。

我真的希望這可以幫助你。您可以使用Fragments構建一個非常靈活的用戶界面。例如,稍後,您可以在運行時用其他片段替換前兩個片段,從而提高靈活性。您甚至可以爲不同的屏幕尺寸設置不同的用戶界面,在手機上可以看到更加緊湊的視圖,但在平板電腦等更大的屏幕上可以提供更多功能。

如果這能幫助你,我很樂意聽到!

相關問題