2012-04-19 37 views
3

需要获取不在主xml文件(我在setContentView()中设置的文件)中的LinearLayout。因此,在写此代码:从另一个xml获取linearLayout Android

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    LinearLayout main = (LinearLayout) findViewById(R.id.main1); 

    LinearLayout oggetto = (LinearLayout) findViewById(R.id.element1); 

    main.addView(oggetto); 
} 

其中部件1写入XML文件:

<?xml version="1.0" encoding="utf-8"?> 
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/element1" 
      android:layout_width="fill_parent" 
      android:orientation="horizontal" 
      android:layout_height="90px" 
      android:background="#000000"> 

      <LinearLayout 
       android:id="@+id/linearLayoutLeftBar" 
       android:layout_width="10px" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:background="#7FFF00" > 
      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/linearLayoutRightContent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical" 
       android:background="#EEEEEE" > 

       <LinearLayout 
        android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" 
        android:layout_height="45px" 
        android:orientation="horizontal" 
        android:background="#EEEEEE" > 

        <LinearLayout 
         android:id="@+id/linearLayoutTxtView1" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:orientation="vertical" 
         android:background="#EEEEEE" 
         android:layout_gravity="right" > 

         <TextView 
          android:id="@+id/textView1" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:text="TextBox1" 
          android:layout_marginLeft="5px" 
          android:textColor="#000000" 
          android:gravity="center" /> 

        </LinearLayout> 

        <LinearLayout 
         android:id="@+id/linearLayoutImgView" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:orientation="vertical" 
         android:gravity="right|center" > 

         <ImageView 
          android:id="@+id/imageView1" 
          android:layout_width="16px" 
          android:layout_height="16px" 
          android:layout_marginRight="16px" 
          android:src="@drawable/ic_launcher" 
          android:background="#FF0000"/> 

        </LinearLayout> 

       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/linearLayout2" 
        android:layout_width="fill_parent" 
        android:layout_height="45px" 
        android:orientation="horizontal" 
        android:background="#EEEEEE" > 

        <LinearLayout 
         android:id="@+id/linearLayoutTxtView1" 
         android:layout_width="wrap_content" 
         android:layout_height="fill_parent" 
         android:orientation="vertical" 
         android:background="#EEEEEE" 
         android:layout_gravity="right" > 

         <TextView 
          android:id="@+id/textView2" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:text="TextBox2" 
          android:layout_marginLeft="5px" 
          android:textColor="#000000" 
          android:gravity="center" /> 

        </LinearLayout> 

        <LinearLayout 
         android:id="@+id/linearLayoutImgView" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:orientation="vertical" 
         android:gravity="right|center" > 

         <TextView 
          android:id="@+id/textView3" 
          android:layout_width="wrap_content" 
          android:layout_height="fill_parent" 
          android:layout_marginRight="16px" 
          android:text="TextBox3" 
          android:textColor="#000000" 
          android:gravity="center|right" /> 

        </LinearLayout> 

       </LinearLayout> 

      </LinearLayout> 
     </LinearLayout> 

问题是,当我启动应用程序,我得到一个错误:无法启动活动ComponentInfo {COM .chiamata/com.chiamata.ChiamataActivity}:java.lang.NullPointerException 这是因为element1为空。如果我把它添加到主Linearlayout之前,如果(element1!= null)一切正常,但显然不会添加element1。我需要以编程方式添加它,所以我不能将所有内容放在一个xml文件中。 我该怎么办? 谢谢,马蒂亚

+0

element1为null here.do您认为您需要说LinearLayout element1 =(LinearLayout)findViewById(R.id.element1); – UVM 2012-04-19 11:21:24

回答

11

如果我正确理解你的问题,你可以使用

LayoutInflater inflater; 
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    

LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.Your_layout_file_with_element1 , null); 

然后你可以使用main.addView(layout);添加外部布局。

+0

Your_layout_file_with_element1的含义 – 2015-10-09 11:26:37

2

你可以说

LinearLayout element1 = (LinearLayout) findViewById(R.id.element1); 

main.addView(element1); 
+0

是的,对不起我在这里写错了,但在java项目中被称为oggetto – pindol 2012-04-19 11:25:24

0

为什么你不能把它放在一个XML中,然后将可见性属性从GONE切换到可见?

+0

我解决了,谢谢:) – pindol 2012-04-19 11:51:55