2011-10-28 27 views
5

我發現了一個簡單的SwipeSample,我改變,讓我創造新的XML佈局和膨脹的主要佈局以顯示它們。我想要做的也是能夠以編程方式爲滑動過程添加布局。膨脹觀點的最好辦法編程

我有main.xml中的佈局和一個red.xml和yellow.xml其是一種簡單的LinearLayout設置爲純色一個TextView。

以下作品中的代碼,但我不認爲這是正確的還是最好的方式做什麼,我試圖讓。 如果任何人都可以建議更好的方式,將不勝感激。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Create a layout with a solid blue background programmatically 
    TextView tv1 = new TextView(this); 
    tv1.setText("Blue"); 
    tv1.setBackgroundColor(Color.BLUE); 
    tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll.addView(tv1); 
    //Create a layout with a solid green background programmatically 
    TextView tv2 = new TextView(this); 
    tv2.setText("Green"); 
    tv2.setBackgroundColor(Color.GREEN); 
    tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    LinearLayout ll2 = new LinearLayout(this); 
    ll2.setOrientation(LinearLayout.VERTICAL); 
    ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll2.addView(tv2); 
    //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts 
    fSpace = (ViewFlipper)findViewById(R.id.flipper); 
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.yellow, fSpace); 
    inflater.inflate(R.layout.red, fSpace); 
    fSpace.addView(ll); 
    fSpace.addView(ll2); 

} 

回答

2

你膨脹R.layout.yellow和R.layout.red的方式的確是正確的方式。您可以通過將其轉移到xml來簡化代碼。我認爲電視1只是一個樣本?如果沒有,它可以進入main.xml。你甚至可以找到一種方法來創建黃色和紅色的單一通貨膨脹......取決於你在做什麼。

編程創建視圖僅僅是,在大多數情況下,稍微繁瑣。

4

如果您有您想要創建一個編程複雜的佈局,這可能是最簡單的辦法XML佈局預製,然後就膨脹,並在運行時添加它。

創建視圖的XML

下面是一個示例預製XML佈局在佈局文件夾中。你可能是任何東西,單一視圖或整個複雜的佈局。

佈局/ my_view.xml

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/TextView1" 
     android:text="This is a TV"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/TextView2" 
     android:text="How are you today?"/> 
</LinearLayout> 

做一個容器視圖

有個地方它把你的觀點在你的活動佈局。你可以有這樣的事情。

<FrameLayout 
    android:id="@+id/flContainer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

</FrameLayout> 

充氣認爲

使用得到的容器的引用,從XML虛增您的觀點,然後將其添加到容器中。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FrameLayout container = (FrameLayout) findViewById(R.id.flContainer); 
    View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false); 
    container.addView(inflatedLayout); 

} 

這樣做可以讓您的代碼更加清潔。

參見: