2012-02-29 37 views
0

朋友,通過addContentView()添加LinearLayout。滑動抽屜現在在最後加入的內容

我有一個xml文件,包括一個滑動抽屜。 我在應用程序的開始setContentView。

我使用幾個TextViews和Edittext動態創建Java中的LinearLayout,並通過addContentView()將其添加到應用程序中。 SlidingDrawer仍然有效,但我通過addContentView()添加的LinearLayout顯示在SlidingDrawer上方。 有沒有辦法讓SlidingDrawer工作正確?我的代碼

繼承人部分

  setContentView(R.layout.real_time_report_layout); 
      LinearLayout linearLayoutRealTimeReportActivity = new LinearLayout(ctx); 


      TextView tv_questionType1 = new TextView(ctx); 

    linearLayoutRealTimeReportActivity.addView(tv_questionType1); 

      addContentView(linearLayoutRealTimeReportActivity, layoutParamsRealTimeReportActivity); 

我很高興對任何的支持也解決!

我的繼承人XML佈局

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/et_real_time_report_description" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <requestFocus /> 
    </EditText> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="20dp" 
     android:orientation="horizontal" 
     android:weightSum="100" > 

     <Button 
      android:id="@+id/b_real_time_report_send" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:text="@string/b_real_time_report_send" > 
     </Button> 

     <Button 
      android:id="@+id/b_real_time_report_cancel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:text="@string/b_real_time_report_cancel" > 
     </Button> 
    </LinearLayout> 
</LinearLayout> 

<SlidingDrawer 
    android:id="@+id/sd_real_time_report_layout_SlidingDrawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:content="@+id/content" 
    android:handle="@+id/sd_real_time_report_layout_handle" > 

    <Button 
     android:id="@+id/sd_real_time_report_layout_handle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Handle" > 
    </Button> 

    <RelativeLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <com.google.android.maps.MapView 
      android:id="@+id/mv_real_time_report" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_marginBottom="50dp" 
      android:apiKey="0IfKjDM6XzpM6rGFR0H6X03Y1aWVBOnJ1C8b6wQ" 
      android:clickable="true" 
      android:enabled="true" /> 

     <TextView 
      android:id="@+id/tv_real_time_report_userLocation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="100dp" 
      android:text="test" 
      android:textSize="20dp" > 
     </TextView> 

     <Button 
      android:id="@+id/b_real_time_report_deleteUserLocationPin" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:text="@string/b_real_time_report_deleteUserLocationPin" /> 

     <ToggleButton 
      android:id="@+id/tb_real_time_report_chooseLocation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:textOff="@string/tb_real_time_report_ChooseLocationOFF" 
      android:textOn="@string/tb_real_time_report_ChooseLocationON" /> 
    </RelativeLayout> 
</SlidingDrawer> 

+1

也發佈您的XML佈局。 – 2012-02-29 14:29:52

回答

2

您需要稍作更改。

  • 給一個ID,你的佈局XML父LinearLayout(比方說,android:id="@+id/parent_container")。
  • 你會得到你的LinearLayout在Java文件中。

是這樣的:

LinearLayout container=(LinearLayout)this.findViewById(R.id.parent_container); 
  • 現在,當增加新的Viewcontainer我們將使用索引視圖放置在特定的指數。

是這樣的:

container.addView(linearLayoutRealTimeReportActivity, container.getChildCount()-2); 

我們中減去-2,因爲我們希望把在查看倒數第二個指標。 因此SlidingDrawer仍然是最後一個索引。

+0

謝謝阿迪爾!我甚至不需要索引。只需給LinearLayout一個id和addView到LinearLayout即可。 – Arnold 2012-02-29 15:15:24

+0

很酷,我在考慮你的'LinearLayout'中有'SlidingDrawer'。快樂編碼.. – 2012-02-29 15:17:45

+0

一個很好的答案,謝謝! – conor 2014-12-09 23:02:50

1

的Android在吸引它們加入到容器的順序查看,所以你的觀點將抽屜下繪製。

最簡單的解決方案可能是將您的cusom視圖添加到xml中定義的FrameLayout(或任何其他佈局)。