2016-09-17 66 views
2

我有要求將編程生成佈局和xml結合起來的需求。我應該採取什麼方法?請告訴我。謝謝。Android:合併xml並以編程方式生成佈局

生成佈局:

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    LinearLayout audio = new LinearLayout(this); 

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER); 

    RecordButton = new RecordButton(this); 
    audio.addView(RecordButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 

    PlayButton = new PlayButton(this); 
    audio.addView(PlayButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 

    setContentView(audio); 
} 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:gravity="top"> 

<Button 
android:id="@+id/next" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="130dp" 
android:background="#54C571" 
android:text="Next" 
android:textColor="#FFFFFF" 
android:textSize="23sp" 
android:textStyle="bold" /> 
+0

'some_view_in_xml.addView(generated_layout);'? –

+0

@KNeerajLal線性佈局怎麼樣?我應該如何包含它們?它是這樣的'audio.xml.addView(生成)'?我試過了,它說xml無法解析 –

+0

顯示你的xml佈局。 –

回答

2

創建一個容器,以便容納您的視圖,並添加生成視圖的是,

這是XML,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:gravity="top"> 

    <Button 
     android:id="@+id/next" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="130dp" 
     android:background="#54C571" 
     android:text="Next" 
     android:textColor="#FFFFFF" 
     android:textSize="23sp" 
     android:textStyle="bold" /> 

    <!-- create a container view to hold your view --> 
    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/next" /> 

</RelativeLayout> 

在你的代碼中,

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.the_above_layout); 

    LinearLayout container = (LinearLayout) findViewById(R.id.container); 


    LinearLayout audio = new LinearLayout(this); 

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER); 

    RecordButton = new RecordButton(this); 
    audio.addView(RecordButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 

    PlayButton = new PlayButton(this); 
    audio.addView(PlayButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 


    // add the view to your container 
    container.addView(audio); 
} 
+1

我現在明白了。謝謝你,我今天學到了新的東西。 –

相關問題