1

LinearLayoutaを anotherの中に挿入しようとしていLinearLayoutます。自分のしていることが正しいかどうかはわかりません。インフレを使用せずに、この方法で試す必要があります。

      LinearLayout address2;
      address2 = new LinearLayout(this);
      address2 = (LinearLayout)findViewById(R.id.sfsp2_layout);

      LinearLayout teste3 = (LinearLayout)findViewById(R.id.se_contentAdressPostal);

      LinearLayout teste4 = (LinearLayout)teste3.findViewWithTag("teste");
      teste4.addView(address2);

LinearLayout teste3

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/se_contentAdressPostal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="2dp"
    android:background="@drawable/background_tile_address_postal"
    android:orientation="vertical" >

    <include
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/socio_form_structured_postal" />

LinearLayout teste4

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfsp_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_tile"
    android:orientation="vertical"
    android:tag="teste" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/sfsp_layout_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="49dp"
        android:layout_gravity="right"
        android:background="@drawable/background_tile"
        android:orientation="horizontal"
        android:tag="teste" >
        <Button
            android:id="@+id/sfsp_btStructuredPostal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:hint="@string/sfsp_btStructuredPostal" /> .......

LinearLayour address2 (レイアウト 4 に挿入する必要があるレイアウト)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sfsp2_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background_tile"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/sfsp2_etStructuredPostalApartado"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="65dp"
        android:layout_marginRight="55dp"
        android:layout_marginTop="2dp"
        android:layout_weight="0.14"
        android:ems="10"
        android:hint="@string/sfsp2_etStructuredPostalApartado"
        android:inputType="textMultiLine"
        android:scrollHorizontally="false" >

LinearLayoutteste4」は「teste3」の中にあります。LinearLayout「teste4」内に「address2」を挿入する必要があります。私が間違っていることを教えてもらえますか?

4

1 に答える 1

2

ここで、その仕組みについて誤解が生じていると思いますfindViewById。以下で説明してみます。これが役に立てば幸いです。そうでない場合はお知らせください。

使用できる可能性があるのは 2 つありますfindViewById()

  1. Activity.findViewById(): これは、現在のアクティビティのメイン コンテンツ ビュー (つまり、 を使用して設定されたビュー) でビューを見つけるために使用できますsetContentView()。それ以外の目的で使用することはできません。たとえば、他のレイアウト ファイルからビューを見つけることはできません。関数には、それを見つける方法を知る方法がありません。私が知る限り、これはあなたが使おうとしている機能です。最後の XML ファイルがメイン レイアウトでない場合、行address2 = (LinearLayout)findViewById(R.id.sfsp2_layout)は失敗します。

  2. View.findViewById(): この関数は、他のビューに含まれるビューを見つけるために使用できます。たとえば、view1を含みview2view2IDを持っている場合、を呼び出すsome_idことで見つけることができます。これが機能するためには、完全に初期化する必要があります。たとえば、の説明が XML の場合、最初に完全にインフレートする必要があります。view2view1.findViewById(R.id.some_id)view1view1

本質的に、別の XML ファイルで記述されているビューを操作する場合は、最初にそれをインフレートする必要があります。それを回避する方法はないと思います。

于 2012-07-25T15:37:29.990 に答える