1

現在のフラグメントのコードを使用して背景色を設定する際に助けが必要です。特定のタブを選択すると、フラグメントが作成されます。

以下は私の主な活動コードです

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() を使用して?

4

2 に答える 2

0

背景色を設定するには:

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

レイアウトをフラグメントにインフレートするには、 onCreateView から View を返す必要があります。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState)     {
    return inflater.inflate(R.layout.your layout, container, false);
 }
于 2013-10-29T12:17:48.473 に答える