2013-08-07 102 views
0

有3個xml文件。 Main.xml具有線性佈局。
Button.xml只是包含一個按鈕。
txt.xml只包含一個編輯文本。
我膨脹button.xmlMain.xml
看起來不錯。
按鈕的尺寸與在Button.xml中的尺寸相同。
接下來我將編輯文本充氣到Main.xml,其中ems = 10(android:ems="10")。
當我運行時,按鈕的大小與編輯文本的大小相同。
這是怎麼發生的以及如何克服這個問題。代碼被粘貼。Android中的佈局充氣器

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" 
     tools:context=".First" /> 
    <LinearLayout 
     android:id="@+id/ll" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    </LinearLayout> 

</LinearLayout> 

Button.xml

<?xml version="1.0" encoding="utf-8"?> 


    <Button xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     /> 

txt.xml

<?xml version="1.0" encoding="utf-8"?> 

    <EditText xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ems="10" 
    > 

     <requestFocus /> 
    </EditText> 

Main.java

public class First extends Activity { 
    LinearLayout lout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_first); 
     final LayoutInflater inf=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     Button btn=(Button)inf.inflate(R.layout.button, null); 
     lout=(LinearLayout)findViewById(R.id.ll); 

     lout.addView(btn); 
     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(lout.getChildAt(5)==null){ 
        EditText e=(EditText)inf.inflate(R.layout.txt, null); 
        lout.addView(e); 
       } 
      } 
     }); 

    } 

點擊5個按鈕,5個編輯文本將顯示出來。但是當我點擊按鈕時,除了編輯文本形成之外,按鈕的大小也會改變。

+0

請在此處添加您的代碼。 –

+0

粘貼代碼 – JayadevKV

回答

0

請粘貼你的代碼在這裏,只有那麼任何人都可以幫助你。您仍然可以將按鈕的layout_width設置爲20sp(根據您的需要)。

在你的Main.xml文件中,你需要修改佈局的寬度,因爲佈局很小,因爲textview很小。但是,當您點擊按鈕時,佈局將按EditText的所需寬度增加,因此按鈕尺寸也會增加:

<LinearLayout android:id="@+id/ll" 
       android:orientation="vertical" 
       android:layout_width="100sp" 
       android:layout_height="wrap_content" > 
</LinearLayout>