简体   繁体   中英

How to use textView with Fragments Android/Java?

public class YourComputerRig extends Fragment {

        public void onCreate(Bundle savedInstanceState) {

            // Create the text view
            TextView textView = new TextView(this);
            textView.setTextSize(30);

            textView.setText("Passed in var: " + variable);

            // Set the text view as the activity layout
            setContentView(textView);
        }

new TextView(this) and setContentView are throwing errors... What would I use as the parameter instead of this? Or would it be better to not use a fragment for what I am trying to do?

overriding onCreateView , that is called to have the fragment instantiate its user interface view.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    TextView textView = new TextView(getActivity());
    textView.setTextSize(30);
    textView.setText("Passed in var: " + variable);
    return textView;
 }

Through getView() you can get the root view for the fragment's layout (the one returned by onCreateView. In your case it will return a TextView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM