3

我想在相对布局中膨胀相对布局。但它只是在膨胀一次之后才膨胀,请帮我看看我的代码有什么问题。

活动代码

LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout main = (RelativeLayout)findViewById(R.id.passenger_details_layout);

        for(int i=0;i<9;i++){           
            View view = getLayoutInflater().inflate(R.layout.passenger_details_layout, main,false);
            main.addView(view);
        }

@Ritaban建议和帮助之后我做了什么

活动代码:

LayoutInflater inflater;

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

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_itineary_page);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        /*
         * LayoutInflater inflater = (LayoutInflater) getBaseContext()
         * .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         */
        LinearLayout main = (LinearLayout) findViewById(R.id.main_passenger_details_layout);

        for (int i = 0; i < 2; i++) {
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout layout = (RelativeLayout) inflater.inflate(
                    R.layout.passenger_details_layout, main, false);
            main.addView(layout, i);
        }
        RelativeLayout lay = (RelativeLayout) findViewById(R.id.thingstoshowbelow);
        RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) lay
                .getLayoutParams();
        params.addRule(RelativeLayout.BELOW, R.id.main_passenger_details_layout);
        lay.setLayoutParams(params);
        lay.setVisibility(View.VISIBLE);
    }

主要的.XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_scroll_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imgLogo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="5dip"
            android:padding="3dip"
            android:src="@drawable/logo_demo" />

        <ScrollView
            android:id="@+id/main_scroll_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/imgLogo" >

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

                <RelativeLayout
                    android:id="@+id/trip_details_main"
                    android:layout_width="fill_parent"
                    android:layout_height="70dp"
                    android:background="@drawable/flight_detail"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/trip_locations"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="5dp"
                        android:text="Delhi to Hydrabad"
                        android:textColor="#FFFFFF"
                        android:textSize="20sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/depart_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/trip_locations"
                        android:layout_marginLeft="5dp"
                        android:text="18 Sep 2013"
                        android:textColor="#FFFFFF"
                        android:textSize="15sp" />

                    <ImageView
                        android:id="@+id/imgRight"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_marginTop="12dp"
                        android:src="@drawable/cal" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/travel_itin_header"
                    android:layout_width="fill_parent"
                    android:layout_height="40dp"
                    android:layout_below="@+id/trip_details_main"
                    android:layout_marginTop="2dp"
                    android:background="@drawable/gray"
                    android:gravity="center_vertical" >

                    <TextView
                        android:id="@+id/itinerary_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="90dp"
                        android:layout_marginTop="5dp"
                        android:text="Itinerary Details"
                        android:textColor="#FFFFFF"
                        android:textSize="15sp"
                        android:textStyle="bold" />
                </RelativeLayout>

                <LinearLayout
                    android:id="@+id/main_passenger_details_layout"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/travel_itin_header"
                    android:layout_marginTop="2dp"
                    android:orientation="vertical" >
                </LinearLayout>

                <RelativeLayout
                    android:id="@+id/thingstoshowbelow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/passenger_details_layout"
                    android:visibility="gone" >

                    <RelativeLayout
                        android:id="@+id/depart_flight_details_duration"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"
                        android:layout_marginTop="2dp"
                        android:background="@drawable/small_search" >

                        <ImageView
                            android:id="@+id/flight_depart_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentTop="true"
                            android:padding="3dip"
                            android:src="@drawable/dep" />

                        <TextView
                            android:id="@+id/depart_return_location"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignTop="@+id/flight_depart_image"
                            android:layout_marginLeft="12dp"
                            android:layout_marginTop="10dp"
                            android:layout_toLeftOf="@+id/arrow_image"
                            android:layout_toRightOf="@+id/flight_depart_image"
                            android:text="Delhi to Hydrabad"
                            android:textColor="#FFFFFF"
                            android:textSize="15sp"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/one_way_date_and_duration_details"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/depart_return_location"
                            android:layout_marginLeft="12dp"
                            android:layout_toRightOf="@+id/flight_depart_image"
                            android:text="18 Sep 2013 09:45 | Duration 2:45"
                            android:textColor="#FFFFFF"
                            android:textSize="12sp" />

                        <ImageView
                            android:id="@+id/arrow_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBottom="@+id/depart_return_location"
                            android:layout_alignParentRight="true"
                            android:layout_marginRight="12dp"
                            android:layout_marginTop="10dp"
                            android:src="@drawable/arrow2" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/travel_agency_header"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/depart_flight_details_duration"
                        android:layout_marginTop="2dp"
                        android:background="@drawable/travelagency_bg" >

                        <TextView
                            android:id="@+id/travel_agency_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="5dp"
                            android:text="Travel Agency"
                            android:textColor="#676767"
                            android:textSize="15sp"
                            android:textStyle="bold" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/tarvel_agency_details"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/travel_agency_header"
                        android:layout_marginTop="2dp" >

                        <ImageView
                            android:id="@+id/agent_logo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:src="@drawable/uniglobe_logo" />

                        <ImageView
                            android:id="@+id/mobile_logo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="8dp"
                            android:layout_marginTop="10dp"
                            android:layout_toRightOf="@+id/agent_logo"
                            android:src="@drawable/mobile" />

                        <ImageView
                            android:id="@+id/at_logo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:layout_marginTop="10dp"
                            android:layout_toRightOf="@+id/mobile_logo"
                            android:src="@drawable/at" />

                        <ImageView
                            android:id="@+id/email_logo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:layout_marginTop="10dp"
                            android:layout_toRightOf="@+id/at_logo"
                            android:src="@drawable/at" />

                        <TextView
                            android:id="@+id/travel_agency_address"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/agent_logo"
                            android:layout_marginLeft="5dp"
                            android:text="XXXXXXXXXXXXXXXXXXXX"
                            android:textColor="#676767" />

                        <TextView
                            android:id="@+id/travel_agency_fax"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/travel_agency_address"
                            android:layout_marginLeft="5dp"
                            android:layout_marginTop="2dp"
                            android:text="Fax : 2345678"
                            android:textColor="#676767" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/genral_remarks_header"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/tarvel_agency_details"
                        android:layout_marginTop="2dp"
                        android:background="@drawable/general_remar" >

                        <TextView
                            android:id="@+id/genral_remark_header_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="5dp"
                            android:text="Genral Remarks"
                            android:textColor="#676767"
                            android:textSize="15sp"
                            android:textStyle="bold" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:id="@+id/genral_remark_text_layout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/genral_remarks_header"
                        android:layout_marginTop="2dp" >

                        <TextView
                            android:id="@+id/genral_remark_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:gravity="center_vertical"
                            android:text="HAVE A NICE FLIGHT"
                            android:textColor="#676767" />
                    </RelativeLayout>
                </RelativeLayout>
            </RelativeLayout>
        </ScrollView>
    </RelativeLayout>

</ScrollView>
4

5 回答 5

3

使用线性布局作为父视图并给出android:orientation = "vertical". 相对布局是一种布局,其中子级的位置相对于彼此或父级进行描述。如果要使用相对布局,则必须使用addrule属性,否则所有子级将被绘制在前一个子级之上.

例如:

<Linearlayout
            android:id="@+id/passenger_details_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/travel_itin_header"
            android:layout_marginTop="2dp"
            android:gravity="center_vertical"
            android:orientation = "vertical" >
        </Linearlayout>

在活动中:

  LayoutInflater inflater = (LayoutInflater)getBaseContext()
                                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           LinearLayout main =(LinearLayout)findViewById(R.id.passenger_details_layout);              

    for(int i=0;i<9;i++){           
        View view = inflater.inflate(R.layout.passenger_details_layout, null);
        main.addView(view);
    }
于 2013-09-19T12:15:18.273 回答
1

好的,这是您的解决方案复制并将其粘贴到您的主 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imgLogo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="5dip"
    android:padding="3dip"
    android:src="@drawable/ic_launcher" />

<ScrollView
    android:id="@+id/main_scroll_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/imgLogo" >

    <RelativeLayout
        android:id="@+id/main_ScrollView_Container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/trip_details_main"
            android:layout_alignParentTop="true"
            android:layout_width="fill_parent"
            android:layout_height="70dp"
            android:background="@android:color/holo_blue_dark"
            android:gravity="center_vertical" >

            <TextView
                android:id="@+id/trip_locations"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="5dp"
                android:text="Delhi to Hydrabad"
                android:textColor="#FFFFFF"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/depart_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/trip_locations"
                android:layout_marginLeft="5dp"
                android:text="18 Sep 2013"
                android:textColor="#FFFFFF"
                android:textSize="15sp" />

            <ImageView
                android:id="@+id/imgRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="12dp"
                android:src="@android:color/holo_blue_bright" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/travel_itin_header"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_below="@+id/trip_details_main"
            android:layout_marginTop="2dp"
            android:background="@android:color/holo_blue_light"
            android:gravity="center_vertical" >

            <TextView
                android:id="@+id/itinerary_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="90dp"
                android:layout_marginTop="5dp"
                android:text="Itinerary Details"
                android:textColor="#FFFFFF"
                android:textSize="15sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/Relative1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/travel_itin_header"
            android:layout_marginTop="2dp"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/main_passenger_details_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:orientation="vertical" >
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/depart_flight_details_duration"
            android:layout_width="wrap_content"
            android:layout_below="@+id/Relative1"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="2dp"
            android:background="@android:color/holo_blue_bright" >

            <ImageView
                android:id="@+id/flight_depart_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:padding="3dip"
                android:src="@android:color/holo_blue_bright" />

            <TextView
                android:id="@+id/depart_return_location"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/flight_depart_image"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="10dp"
                android:layout_toLeftOf="@+id/arrow_image"
                android:layout_toRightOf="@+id/flight_depart_image"
                android:text="Delhi to Hydrabad"
                android:textColor="#FFFFFF"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/one_way_date_and_duration_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/depart_return_location"
                android:layout_marginLeft="12dp"
                android:layout_toRightOf="@+id/flight_depart_image"
                android:text="18 Sep 2013 09:45 | Duration 2:45"
                android:textColor="#FFFFFF"
                android:textSize="12sp" />

            <ImageView
                android:id="@+id/arrow_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/depart_return_location"
                android:layout_alignParentRight="true"
                android:layout_marginRight="12dp"
                android:layout_marginTop="10dp"
                android:src="@android:color/holo_blue_bright" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/travel_agency_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/depart_flight_details_duration"
            android:layout_marginTop="2dp"
            android:background="@android:color/holo_blue_bright" >

            <TextView
                android:id="@+id/travel_agency_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:text="Travel Agency"
                android:textColor="#676767"
                android:textSize="15sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/tarvel_agency_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/travel_agency_header"
            android:layout_marginTop="2dp" >

            <ImageView
                android:id="@+id/agent_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:src="@android:color/holo_blue_bright" />

            <ImageView
                android:id="@+id/mobile_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/agent_logo"
                android:src="@android:color/holo_blue_bright" />

            <ImageView
                android:id="@+id/at_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/mobile_logo"
                android:src="@android:color/holo_blue_bright" />

            <ImageView
                android:id="@+id/email_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="10dp"
                android:layout_toRightOf="@+id/at_logo"
                android:src="@android:color/holo_blue_bright" />

            <TextView
                android:id="@+id/travel_agency_address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/agent_logo"
                android:layout_marginLeft="5dp"
                android:text="XXXXXXXXXXXXXXXXXXXX"
                android:textColor="#676767" />

            <TextView
                android:id="@+id/travel_agency_fax"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/travel_agency_address"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="2dp"
                android:text="Fax : 2345678"
                android:textColor="#676767" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/genral_remarks_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tarvel_agency_details"
            android:layout_marginTop="2dp"
            android:background="@android:color/holo_blue_bright" >

            <TextView
                android:id="@+id/genral_remark_header_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:text="Genral Remarks"
                android:textColor="#676767"
                android:textSize="15sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/genral_remark_text_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/genral_remarks_header"
            android:layout_marginTop="2dp" >

            <TextView
                android:id="@+id/genral_remark_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:gravity="center_vertical"
                android:text="HAVE A NICE FLIGHT"
                android:textColor="#676767" />
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

并为活动放这个

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LinearLayout main = (LinearLayout) findViewById(R.id.main_passenger_details_layout);

    for (int i = 0; i < 9; i++) {
        LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.activity_second, main, false);
        main.addView(row);
    }
}
于 2013-09-19T12:05:13.777 回答
1

LayoutInflater 正在工作,并且正在添加九次布局。但是您看不到所有视图,因为所有布局都添加到另一个 RelativeLayout,因此它们将一个添加到另一个。

因此,为了克服在父视图中使用具有垂直方向而不是 Relativelayout 的 LinearLayout

于 2013-09-19T12:15:46.487 回答
1

每次您将视图添加到该相对布局时,它都会添加到相同的位置..就像视图堆栈一样..你看不到下面的视图..最好尝试LinearLayout .. 视图将水平或垂直添加..so你可以看到那九个视图..

于 2013-09-19T12:21:57.757 回答
1
// Replace this line
 View view = getLayoutInflater().inflate(R.layout.passenger_details_layout, main,false);
                                             to
 View view = getLayoutInflater().inflate(R.layout.passenger_details_layout, null);                             
于 2013-09-19T12:26:50.240 回答