在android中充气布局

时间:2013-12-03 08:22:35

标签: android xml

您好我想在另一个布局中充气xml布局。我正确地做了但它没有正确到来。因为我在其中充气布局的页面包含2个相对布局。我为两个相对布局提供了不同的id android:id="@+id/stocklist"android:id="@+id/gifts"。 stocklist布局包含一个+按钮,当我们点击+按钮时,它必须将下面的布局膨胀到它。当我在库存列表布局中膨胀另一个布局时,膨胀的xml没有正确地分配,它不会在父布局下面。如果有人知道,请帮助我。我在这里添加了我的代码和布局。

我的父布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="704dp"
            android:orientation="vertical" >

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView8"
        android:layout_alignBottom="@+id/textView8"
        android:layout_toRightOf="@+id/textView7"
        android:text="Quantity"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <RelativeLayout
        android:id="@+id/stocklist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:layout_marginTop="11dp"
        android:orientation="vertical" >

        <Spinner
            android:id="@+id/spinproducts"
            android:layout_width="175dp"
            android:layout_height="40dp"
            android:layout_marginTop="13dp"
            android:ems="10"
            android:hint="Stocklist Name" >
        </Spinner>

        <EditText
            android:id="@+id/editText1"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:layout_alignTop="@+id/spinproducts"
            android:layout_marginLeft="146dp"
            android:layout_toRightOf="@+id/spinproducts"
            android:ems="10"
            android:inputType="number"
            android:hint="Qty" />

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/editText1"
            android:layout_marginRight="136dp"
            android:layout_toLeftOf="@+id/spindetail" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="40dp"
            android:layout_height="45dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="11dp"
            android:onClick="onAddNewClicked"
            android:text="+" />

        <Spinner
            android:id="@+id/spindetail"
            android:layout_width="180dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="50dp"
            android:layout_toLeftOf="@+id/button1"
            android:ems="10"
            android:hint="Remarks" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/gifts"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_below="@+id/textView12"
        android:layout_marginTop="11dp"
        android:orientation="vertical" >

        <Spinner
            android:id="@+id/spingift"
            android:layout_width="375dp"
            android:layout_height="40dp"
            android:layout_marginTop="13dp"
            android:ems="10"
            android:hint="Stocklist Name" >
        </Spinner>

        <EditText
            android:id="@+id/etgiftqty"
            android:layout_width="500dp"
            android:layout_height="40dp"
            android:layout_alignBottom="@+id/spingift"
            android:layout_marginLeft="33dp"
            android:layout_toRightOf="@+id/spingift"
            android:ems="10"
            android:inputType="number"
            android:hint="Qty" >
        </EditText>

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="40dp"
            android:layout_height="45dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:text="+" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView12"
        android:layout_below="@+id/gifts"
        android:layout_marginTop="33dp"
        android:text="Call Outcome :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView14"
        android:layout_below="@+id/textView14"
        android:layout_marginTop="20dp"
        android:text="Follow Up:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/stocklist"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="16dp"
        android:text="Gifts / Others :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView12"
        android:layout_below="@+id/textView7"
        android:layout_marginTop="12dp"
        android:text="Product"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/textView13"
        android:layout_alignRight="@+id/editText8"
        android:layout_below="@+id/textView13"
        android:layout_marginTop="12dp"
        android:ems="10" >

    </EditText>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner4"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="13dp"
        android:text="Dr.Not Available" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText6"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="16dp"
        android:text="Add Call" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="51dp"
        android:layout_toRightOf="@+id/button3"
        android:text="Cancel" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/spinner1"
        android:layout_toLeftOf="@+id/textView9"
        android:text="In Time:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <MultiAutoCompleteTextView
        android:id="@+id/multiAutoCompleteTextView1"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/spinner3"
        android:layout_marginRight="17dp"
        android:ems="10" >

    </MultiAutoCompleteTextView>

    <EditText
        android:id="@+id/editText8"
        android:layout_width="130dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/editText7"
        android:layout_alignBottom="@+id/editText7"
        android:layout_alignRight="@+id/multiAutoCompleteTextView1"
        android:ems="10" 
        android:hint="Notes">

    </EditText>

    <EditText
        android:id="@+id/editText6"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/spinner3"
        android:layout_alignTop="@+id/spinner4"
        android:ems="10" 
        android:hint="Amount">

    </EditText>

    <EditText
        android:id="@+id/editText7"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/editText6"
        android:layout_alignBottom="@+id/editText6"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@+id/editText8"
        android:ems="10" 
        android:hint="date"/>

    <TextView
        android:id="@+id/textView17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView15"
        android:layout_toLeftOf="@+id/editText7"
        android:text="Spon.Req"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_below="@+id/spinner1"
        android:text="Detailing and Samples Dispensed :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="180dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/textView7"
        android:layout_below="@+id/textView2" />

    <TextView
        android:id="@+id/textView14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText3"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="20dp"
        android:text="Schedule Follow"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="12dp"
        android:text="Location of Visit"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:text="Doctor Name"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#008000"
        android:textSize="35dp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/textView15"
        android:layout_alignBottom="@+id/textView15"
        android:layout_alignLeft="@+id/editText5"
        android:ems="10" >

    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/multiAutoCompleteTextView1"
        android:layout_marginLeft="41dp"
        android:layout_toRightOf="@+id/button4"
        android:text="Work with"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView9"
        android:layout_alignBottom="@+id/textView9"
        android:layout_marginRight="38dp"
        android:layout_toLeftOf="@+id/textView17"
        android:text="Detailed"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="123dp"
        android:layout_height="38dp"
        android:layout_alignLeft="@+id/textView9"
        android:layout_alignTop="@+id/textView5"
        android:layout_marginLeft="22dp" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/editText4"
        android:layout_marginLeft="29dp"
        android:layout_marginTop="11dp"
        android:layout_toRightOf="@+id/spinner1"
        android:ems="10" />

    <TextView
        android:id="@+id/textView16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText5"
        android:layout_alignBottom="@+id/editText5"
        android:layout_alignLeft="@+id/textView15"
        android:text="Follow Up Actions:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/stocklist"
        android:layout_alignLeft="@+id/editText7"
        android:layout_marginLeft="25dp"
        android:text="Discussion Topics"       
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView10"
        android:layout_below="@+id/textView2"
        android:text="Out Time:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="123dp"
        android:layout_height="38dp"
        android:layout_above="@+id/textView7"
        android:layout_toLeftOf="@+id/textView11" />

    <Spinner
        android:id="@+id/spinner4"
        android:layout_width="145dp"
        android:layout_height="40dp"
        android:layout_alignRight="@+id/textView10"
        android:layout_alignTop="@+id/editText5" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/spinner3"
        android:layout_alignLeft="@+id/spinner4"
        android:text="Duration of Visit"
        android:textAppearance="?android:attr/textAppearanceMedium" />

 </RelativeLayout>
    </ScrollView>
</RelativeLayout>

enter image description here

必须夸大的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >

    <RelativeLayout       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:layout_marginTop="11dp"
        android:orientation="vertical" >

        <Spinner
            android:id="@+id/inflatespinproducts"
            android:layout_width="175dp"
            android:layout_height="40dp"
            android:layout_marginTop="13dp"
            android:ems="10"
            android:hint="Stocklist Name" >
        </Spinner>

        <EditText
            android:id="@+id/inflateeditText1"
            android:layout_width="150dp"
            android:layout_height="40dp"
            android:layout_alignTop="@+id/inflatespinproducts"
            android:layout_marginLeft="146dp"
            android:layout_toRightOf="@+id/inflatespinproducts"
            android:ems="10"
            android:inputType="number"
            android:hint="Qty" />

        <CheckBox
            android:id="@+id/inflatecheckBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/inflateeditText1"
            android:layout_marginRight="136dp"
            android:layout_toLeftOf="@+id/inflatespindetail" />

        <Button
            android:id="@+id/inflatebutton1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="40dp"
            android:layout_height="45dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="11dp"
            android:onClick="onDeleteClicked"
            android:text="-" />

        <Spinner
            android:id="@+id/inflatespindetail"
            android:layout_width="180dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="50dp"
            android:layout_toLeftOf="@+id/inflatebutton1"
            android:ems="10"
            android:hint="Remarks" />

    </RelativeLayout>
</LinearLayout>

在对布局进行充气后,我的页面如下所示:

enter image description here

http://i.stack.imgur.com/SOWBJ.png“&GT;

我的源代码:

package abts.medismo.e_detailing;

import abts.medismo.e_detailing.Model.Spinmodel;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class addcall extends Activity {
    CheckBox chkdetailed;
    TextView txtdocname, txtdisctopic;
    MultiAutoCompleteTextView autoWorkwith;
    Spinner spinlocation, intime, outtime, spinproducts, spingift, spindetail,
            spinSponsorship;
    String docid, docname, addid, addname, straddid, straddname, spaddid,
            spaddname, dbcatid, dbcatname;
    String strcatid, strcatname, spcatid, spcatname;
    String spinlocationvalue, spingiftvalue, spinintimevalue, spinouttimevalue,
            spinproductvalue, spinworkwithvalue, spindetailvalue,
            spinsponsorshipvalue;
    Intent bgIntent;
    EditText etprodqty,etgiftqty;
    RelativeLayout relStocklist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dupaddcall);                
        autoWorkwith = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
        autoWorkwith
                .setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        etprodqty=(EditText)findViewById(R.id.editText1);
        etgiftqty=(EditText)findViewById(R.id.etgiftqty);
        relStocklist=(RelativeLayout)findViewById(R.id.stocklist);
        spinlocation = (Spinner) findViewById(R.id.spinner1);
        spindetail = (Spinner) findViewById(R.id.spindetail);
        spinSponsorship = (Spinner) findViewById(R.id.spinner4);
        chkdetailed = (CheckBox) findViewById(R.id.checkBox1);
        chkdetailed.setChecked(true);
        intime = (Spinner) findViewById(R.id.spinner2);
        outtime = (Spinner) findViewById(R.id.spinner3);
        spinproducts = (Spinner) findViewById(R.id.spinproducts);
        spingift = (Spinner) findViewById(R.id.spingift);
        txtdocname = (TextView) findViewById(R.id.textView1);
        txtdisctopic = (TextView) findViewById(R.id.textView11);
        spindetail.setEnabled(false);
        bgIntent = getIntent();
        docid = bgIntent.getStringExtra("docid");
        docname = bgIntent.getStringExtra("docname");
        txtdocname.setText(docname);
        DatabaseHandler dbh = new DatabaseHandler(addcall.this);
        SQLiteDatabase db = dbh.getWritableDatabase();
        addid = null;
        addname = null;


    }

    public void onAddNewClicked(View v) {

        inflateEditRow(null);
        //v.setVisibility(View.VISIBLE);        
    }

    private void inflateEditRow(String name) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.inflateaddproduct, null);

        // A TextWatcher to control the visibility of the "Add new" button and
        // handle the exclusive empty view.

        // Inflate at the end of all rows but before the "Add new" button
        relStocklist.addView(rowView, relStocklist.getChildCount());
    }

    public void onDeleteClicked(View v) {
        relStocklist.removeView((View) v.getParent());
    }

    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(addcall.this, DCR.class);
        intent.putExtra("planid", bgIntent.getStringExtra("planid"));
        intent.putExtra("tourdate", bgIntent.getStringExtra("tourdate"));
        final int result = 1;
        startActivityForResult(intent, result);
        finish();
    }

}

1 个答案:

答案 0 :(得分:0)

您应该将相对库存列表布局更改为线性布局,这样膨胀的布局会自动排列在彼此之下。

<LinearLayout
    android:id="@+id/stocklist"
    android:orientation="vertical"
    ... >
    ...
</LinearLayout>