2011-11-20 80 views
0

我需要編輯一個xml文件,問題是我有多個xml,並且在我所有的類/活動中,我無法將xml作爲主視圖。Android XML原始訪問

因此,我必須使用xml文件,而不將它設置爲任何活動的主視圖。甚至有可能嗎?

我的意思是我可以:查看j =(查看)findViewById(R.layout.mytest);

但是,如何在mytest.xml文件中編輯ex的按鈕文本?

+0

我不確定是否正確理解問題。但是,也許[LayoutInfater](http://developer.android.com/reference/android/view/LayoutInflater.html)是你正在尋找的。看看這個[SO問題](http://stackoverflow.com/questions/3477422/layout-inflater-in-android)。 – Piotr

回答

0

儘管您可以使用LayoutInflator來膨脹任何佈局,並對該實例進行任何更改,但是您無法在佈局中查找持久性更改。所以首先setContentView,然後只改變該視圖。

0

所以你說你想動態編輯佈局XML文件???例如,你有一個像...

<LinearLayout> 
    ... 
    <Button 
     android:id="@+id/myButton" 
     ... 
     android:text="Some text" /> 
</LinearLayout> 

...並且你想在運行時動態編輯android:text="Some text"

如果是這樣,那不是這樣做的。只需將android:text屬性保留在佈局xml中,並在運行時設置按鈕的文本即可。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContenView(R.layout.mytest); 

    Button btn = (Button) findViewById(R.id.myButton); 
    btn.setText("Whatever"); 
}