2014-03-27 50 views
0

當更改第一個視圖的TextView的文本時,第二個視圖的textView的文本顯示兩個TextView的文本,一個在另一個之上。使用相同的XML膨脹2個視圖:第二個視圖包含第一個視圖的屬性

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FrameLayout rl=(FrameLayout) findViewById(R.id.mainLayout); 

    View v1=LayoutInflater.from(this).inflate(R.layout.text_view, null); 
    View v2=LayoutInflater.from(this).inflate(R.layout.text_view, null); 

    TextView myTextView1= (TextView) v1.findViewById(R.id.myTextView); 
    TextView myTextView2= (TextView) v2.findViewById(R.id.myTextView); 

    myTextView1.setText("str1"); 
    myTextView2.setText("str2"); 


    rl.addView(v1); 
    rl.addView(v2); 









} 
+2

佈局?碼?有東西要看? –

+0

您正在使用framelayout,是否有意見重疊的目標? – MikeIsrael

+0

不,我在選項卡中使用它,每個視圖屬於不同的選項卡,我沒有發佈我的所有代碼。 – user1582281

回答

0

我猜這個問題是因爲你使用的FrameLayout這樣子視圖會重疊。例如,如果您將該framelayout切換爲線性佈局,我想可以根據文字查看結果如您所期望的那樣。

+0

我不能使用LinearLayout,我的2個視圖中的每一個都屬於不同的選項卡,它們不應該重疊,似乎系統在從1選項卡移動到另一選項卡時不會清除屏幕。 – user1582281

0

很簡單.. 1.你必須使用append屬性,而不是在settext TextView的第二第二 可以使用額外使用意圖通過或使用getter setter方法類似

public static void setstr_name(String str_name) { 
     GlobalVariable.str_name = str_name; 
    } 

    public static String getstr_name() { 
     return str_name; 
    } 
+0

我不明白,爲什麼我需要傳遞意圖,全局變量代表什麼? – user1582281

相關問題