2

このレイアウトファイルには、ユーザーの場所の詳細が含まれています。ユーザーは、1〜3の場所フォームを追加する必要がある条件に応じて、1〜3の場所を持つことになっています。

ここに画像の説明を入力してください

このxmlレイアウトファイルを他のレイアウトファイルに動的に追加する方法を誰かに教えてもらえますか?

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="@drawable/container"
        android:padding="2dp"
        android:layout_below="@+id/upperBox"
        android:layout_margin="5dp"
        >
        <RelativeLayout 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="@drawable/container_inner"
        android:padding="1dp"
        >

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:background="@color/white"
                android:orientation="vertical"
                android:scrollbars="vertical" >

                <TextView
                    style="@style/footerText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tracking_settings" />

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/locationName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/location_name" />

                    <EditText
                        android:id="@+id/editTextLocationName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:inputType="text" >

                        <requestFocus />
                    </EditText>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/address"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/address" />

                    <EditText
                        android:id="@+id/editextAddress"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:inputType="text" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/alertPref"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/alert_preference" />

                    <Spinner
                        android:id="@+id/spinnerAlertPref"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/alert_pref_arrays"
                        android:prompt="@string/alert_pref_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/frequency"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/frequency" />

                    <Spinner
                        android:id="@+id/spinnerFreq"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/freq_arrays"
                        android:prompt="@string/freq_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/TrackBetween"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/track_between" />

                    <Spinner
                        android:id="@+id/spinnerTimeFrom"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/time_arrays"
                        android:prompt="@string/time_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <Spinner
                        android:id="@+id/spinnerTimeFrom1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/am_pm_arrays"
                        android:prompt="@string/am_pm_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/to"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/to" />

                    <Spinner
                        android:id="@+id/spinnerTimeTo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/time_arrays"
                        android:prompt="@string/time_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <Spinner
                        android:id="@+id/spinnerTimeTo1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/am_pm_arrays"
                        android:prompt="@string/am_pm_prompt" />
                </TableRow>

                <TextView
                    style="@style/footerText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/alert_settings" />

                <TableRow
                    android:id="@+id/tableRow9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/sexOffenderIsWithin"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:text="@string/sex_offender_is_within" />

                    <Spinner
                        android:id="@+id/spinnerSexOff"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/sex_offender_arrays"
                        android:prompt="@string/sex_offender_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow10"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/travelAwayFromLocation"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:text="@string/travel_away_from_location" />

                    <Spinner
                        android:id="@+id/spinnerTravelAway"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/sex_offender_arrays"
                        android:prompt="@string/sex_offender_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/arriveAtLocation"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/arrive_at_location" />

                    <Spinner
                        android:id="@+id/spinnerArriveAt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/yes_donttrack_arrays"
                        android:prompt="@string/yes_donttrack_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow12"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/travelByVehicle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/travel_by_vehicle" />

                    <Spinner
                        android:id="@+id/spinnerTravelBy"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/yes_donttrack_arrays"
                        android:prompt="@string/yes_donttrack_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/phonStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/phon_status" />

                    <Spinner
                        android:id="@+id/spinnerPhonStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:entries="@array/phon_status_arrays"
                        android:prompt="@string/phon_status_prompt" />
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow14"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        style="@style/footerText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/cancel" />

                    <TextView
                        style="@style/footerText"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:text="@string/submit" />
                </TableRow>
            </TableLayout>

            </RelativeLayout>
            </RelativeLayout>
4

3 に答える 3

2

LayoutInflaterを介した別のレイアウトのこのレイアウトファイル。

このレイアウトをメインレイアウトに追加するとします。したがって、メインレイアウトで線形レイアウトを取り、このリストレイアウトをメインレイアウトで取得します。これがmainlayout.add(view)LayoutInflaterviwviewです。

例 :

private View view;
    view = getLayoutInflater().inflate(R.layout.image_sub_layout,
                null);
     topicLinearLayout.addView(view); // main Layout Linear layout

ありがとう。

于 2012-10-24T10:01:49.013 に答える
2

main.xml空のlinearLayoutである、を使用した最初のアクティビティがあるとします。

経由LayoutInflaterであなたはそのレイアウトを次のように得ることができます

linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
layoutToAdd = (LinearLayout) getLayoutInflater().inflate(R.layout.layout_to_add, null);

次に、目的のxml-layout-fileをそのlinearLayoutに追加するだけです(他のタイプのレイアウト、単一ViewのsまたはViewGroupsについても同じです)。

   linearLayout.add(layoutToAdd);

または、Abdulが提案したように、目的のレイアウトをビューに「キャスト」します。

于 2012-10-24T10:04:19.243 に答える
0

私の理解では、実装する条件に応じて、レイアウトに1〜3のロケーションフォームを含める必要がありました。UIから入力するロケーションの数を処理していることが確実な場合は、同じものを追加するだけです。異なるIDで3回レイアウトVisibilityGONEます。view.VISIBLEまた、UIから処理しながら、UIスレッドからの条件に応じてレイアウトの表示を変更できます。

scrollViewのコードを挿入して、メインレイアウトをエンドユーザーがスクロールできるようにすることができます。

于 2012-10-24T10:08:18.543 に答える