通过addContentView()添加LinearLayout.滑动抽屉现在位于最后添加的内容下方



朋友,

我有一个xml文件,其中包括一个滑动抽屉。我在应用程序的开头设置了ContentView。

我用Java动态创建了一个LinearLayout,其中包含一些TextViews和Edittext,并通过addContentView()将其添加到应用程序中。SlidingDrawer仍然有效,但通过addContentView()添加的LinearLayout i显示在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>

您需要进行一些细微的更改。

  • 在布局xml中为父级LinearLayout提供一个id(比如android:id="@+id/parent_container"
  • 您将获得java文件中的LinearLayout

像这样的东西:

LinearLayout container=(LinearLayout)this.findViewById(R.id.parent_container);
  • 现在,当将新的View添加到container时,我们将使用索引将视图放在特定的索引处

像这样的东西:

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

我们减去了-2,因为我们想把View放在倒数第二个索引处。所以SlidingDrawer仍然处于最后一个索引。

Android按照将视图添加到容器的顺序绘制视图,因此您的视图将在Drawer下绘制。

最简单的解决方案可能是将cusom视图添加到用xml定义的FrameLayout(或任何其他布局)中。

最新更新