2013-10-29 28 views
1

我需要一些幫助來使用當前片段的代碼設置背景顏色。片段是在選擇特定選項卡時創建的。從程序設置背景顏色而不是從xml中獲取片段

下面是我的主要活動代碼

public class TabActionBarActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   

    // Set the text and background colour 
    setTheme(R.style.MyTheme); 
    ActionBar actionBar = getActionBar(); 



    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 



    String label6 = getResources().getString(R.string.label6); 
    tab = actionBar.newTab(); 
    tab.setText(label6); 
    TabListener<Tab6Fragment> tl6 = new TabListener<Tab6Fragment>(this, 
      label6, Tab6Fragment.class); 
    tab.setTabListener(tl6); 
    actionBar.addTab(tab); 

    String label7 = getResources().getString(R.string.label7); 
    tab = actionBar.newTab(); 
    tab.setText(label7); 
    TabListener<Tab7Fragment> tl7 = new TabListener<Tab7Fragment>(this, 
      label7, Tab7Fragment.class); 
    tab.setTabListener(tl7); 
    actionBar.addTab(tab);   


} 

現在的Tab7Fragment類的代碼看起來像這樣

public class Tab7Fragment extends Fragment { 

    TextView tv1; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) {   

     ScrollView sv = new ScrollView(this.getActivity()); 

     LinearLayout ll = new LinearLayout(this.getActivity()); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     sv.addView(ll); 

     TextView tv = new TextView(this.getActivity()); 
     tv.setText("Dynamic layouts ftw!"); 
     ll.addView(tv); 

     EditText et = new EditText(this.getActivity()); 
     et.setText("weeeeeeeeeee~!"); 
     ll.addView(et); 

     Button b = new Button(this.getActivity()); 
     b.setText("I don't do anything, but I was added dynamically. :)"); 
     ll.addView(b); 

     for(int i = 0; i < 20; i++) { 
      CheckBox cb = new CheckBox(this.getActivity()); 
      cb.setText("I'm dynamic!"); 
      ll.addView(cb); 
     } 
     return this.getView(); 
    } 

}

我現在該怎麼可以設置這個片段的看法?

this.getActivity().setContentView(sv); 

我知道上面的方法是不正確的。但我需要設置滾動視圖佈局的內容視圖。而另一個問題是我如何設置背景顏色爲這種觀點(使用setBackgroundColor()

+1

嘗試sv.setBackgroundColor(getRessources()getColors(R.color.yourcolor)); – Angudroid

+0

感謝您設置顏色的回覆。怎麼樣設置這個片段的佈局?我不能使用來自fragemnt的setContentView(sv) – vineeth

+0

我在下面的回覆中提出了我的建議,如果它對您有用,請接受它;) – Angudroid

回答

0

試試這個代碼在你onCreateView():??

View view = inflater.inflate(R.layout.your_fragment_layout, container, false); 

然後不要忘記返回認爲你只是膨脹到Android運行時

刪除return this.getView();並添加return view;

+0

感謝您的回覆。我沒有這個片段的xml佈局。我動態生成這個佈局,所以不能使用膨脹方法。 – vineeth

0

要設置背景色:

sv.setBackgroundColor(getRessources().getColors(R.color.yourcolor)); 

膨脹佈局成一個片段,你需要返回從onCreateView一個觀點:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  { 
    return inflater.inflate(R.layout.your layout, container, false); 
} 
+0

我想從代碼而不是xml創建這個片段的佈局。所以我不能使用膨脹方法。 – vineeth

+0

我明白你想要什麼,但我不是片段的專家,對不起......你可以在這裏找到有用的方法http://developer.android.com/training/basics/fragments/index.html – Angudroid