2013-04-30 219 views
0

我有一個問題。 我剛剛創建了一個應用程序,可以在其中瀏覽選項卡和部分。 我如何告訴eclipse加載「main.xml」佈局,而不是使用文本視圖創建新的線性佈局? 是否有可能的傢伙?標籤和片段android eclipse

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class FirstTabFragment extends Fragment 
{ 

private Activity activity; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    this.activity = getActivity(); 


    LinearLayout linearLayout = new LinearLayout(activity); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    linearLayout.setLayoutParams(params); 

    TextView textView = new TextView(activity); 
    textView.setText("This is a sample fragment. I am programmatically added"); 
    linearLayout.addView(textView); 

    return linearLayout; 
} 
    } 

我的main.xml代碼:

<RelativeLayout 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" 
tools:context=".MainActivity" 
android:background="@drawable/screenshot1" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="167dp" 
    android:text="Button" /> 

<Button 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/button1" 
    android:layout_below="@+id/button1" 
    android:layout_marginTop="44dp" 
    android:text="Button" /> 

+0

和您的main.xml的內容是什麼? – 2013-04-30 14:31:44

+0

檢查編輯過的plz ...並感謝回覆 – Charbel 2013-04-30 14:34:54

回答

1
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 
     return null; 
    } 
    return (RelativeLayout) inflater.inflate(R.layout.main, container,false); 
} 
+0

非常感謝你的人......你讓我的一天 – Charbel 2013-04-30 15:54:59

0
final LayoutInflater inflater = (LayoutInflater)getSystemService("layout_inflater"); 
TextView textView = (TextView)inflater.inflate(R.layout.main,null);