如何膨胀片段内的视图 [英] How to inflate view inside fragment

查看:178
本文介绍了如何膨胀片段内的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我试图在片段中膨胀视图,我会得到NULL ..例如:

  public View onCreateView LayoutInflater inflater,ViewGroup容器,
Bundled savedInstanceState){
//这里我将使用布局ID&返回。
返回视图;
}

每当点击一个按钮时,我需要创建一个动态视图,例如:button& amp ;添加到LinearLayout。我想在我的片段类中执行此操作,如下所示:

  public void addPlaces(){
Button按钮= new Button(null);
button.setText(button name);
//例如像添加按钮在这里输入代码线性布局
linearLayout.addView(button);





$ b因此,如果我在onCreateView中使用LinearLayout并在add类中使用它,我得到NULL。如何实现?

解决方案

将变量声明为实例变量,然后初始化线性布局

  LinearLayout linearLayout; 
$ b $ public查看onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment1,container,false);
linearLayout =(LinearLayout)rootView.findViewById(R.id.linearlayout);
返回rootView;

然后

  public void addPlaces(){
Button button = new Button(getActivity());
//需要活动上下文
//活动托管的片段。使用getActivity()来获取托管活动的上下文。
button.setText(button name);
linearlayout.addView(button);
}

示例:根据您的要求修改以下内容。



fragment1.xml

 <?xml version = 1.0encoding =utf-8?> 
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =match_parent >

Myfragment.java

  public class Myfragment extends Fragment {

LinearLayout linearLayout;
查看rootView;

@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Button b =(Button)rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
$ b $ @Override
public void onClick(View v){
// TODO自动生成的方法存根
addPlaces();
}

});
linearLayout =(LinearLayout)rootView.findViewById(R.id.linearlayout);

$ b @Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundled savedInstanceState){
rootView = inflater.inflate(R.layout .fragment1,container,false);
返回rootView;
}

public void addPlaces(){
Button button = new Button(getActivity()); //需要活动上下文
button.setText(button name);
linearLayout.addView(button);


我的模拟器快照





编辑:

activity-main.xml

 < RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com/工具
android:layout_width =match_parent
android:layout_height =match_parent
android:paddingBottom =@ dimen / activity_vertical_margin
android:paddingLeft =@ dimen / activity_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context =。MainActivity>

< fragment android:name =com.example.fragments.Myfragment
android:id =@ + id / frag
android:layout_above =@ id / button1
android:layout_width =fill_parent
android:layout_height =fill_parent/>

MainActivity.java

  public class MainActivity extends FragmentActivity {
Button b;
Myfragment片段;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new Myfragment();
fragmentTransaction.add(R.id.frag,fragment);
fragmentTransaction.commit();
b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
$ b $ @Override
public void onClick(View v){
// TODO自动生成的方法存根
fragment.addPlaces();
}

});
}
}

Myfragment.java

  public class Myfragment extends Fragment {

LinearLayout linearLayout;
查看rootView;

@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
linearLayout =(LinearLayout)rootView.findViewById(R.id.linearlayout);

$ b @Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
Bundled savedInstanceState){
rootView = inflater.inflate(R.layout .fragment1,container,false);
返回rootView;
}

public void addPlaces(){
Button button = new Button(getActivity()); //需要活动上下文
button.setText(button name);
linearLayout.addView(button);
}
}


If I try to inflate a view within a fragment I am getting NULL.. For example:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Here I will inflate my view using the layout ID & return.
    return view;
}

Whenever a button is clicked I need to create a dynamic view e.g.: button & add to the LinearLayout. I would like to perform this operation inside my fragment class like this:

public void addPlaces() {    
    Button button = new Button(null);
    button.setText("button name");
    // e.g. like adding button to enter code here linear layout
    linearLayout.addView(button); 
}

So, if I get inflate LinearLayout inside onCreateView and use it in add class, I'm getting NULL. How to achieve?

解决方案

Declare the variable as a instance variable and then initialize Linear Layout

LinearLayout linearLayout;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    return rootView;
}

Then

public void addPlaces() {
    Button button = new Button(getActivity());
    // needs activity context
    // fragment hosted by a activity. use getActivity() to get the context of the hosting activity. 
    button.setText("button name");
    linearlayout.addView(button);
}

Example: Modify the below according to your requirement.

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:id="@+id/linearlayout"
        android:layout_height="fill_parent"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

Myfragment.java

public class Myfragment extends Fragment {

    LinearLayout linearLayout;
    View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Button b = (Button) rootView.findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                addPlaces();
            }

        });
        linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }

    public void addPlaces() {
        Button button = new Button(getActivity()); // needs activity context
        button.setText("button name");
        linearLayout.addView(button);
    }
}

Snap shot of my emulator

Edit :

activity-main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

     <fragment android:name="com.example.fragments.Myfragment"
            android:id="@+id/frag"
            android:layout_above="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

      <Button
          android:id="@+id/button1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:text="Button" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends FragmentActivity {
    Button b;
    Myfragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragment = new Myfragment();
        fragmentTransaction.add(R.id.frag, fragment);
        fragmentTransaction.commit();
        b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                fragment.addPlaces();
            }

        });
    }
}

Myfragment.java

public class Myfragment extends Fragment {

    LinearLayout linearLayout;
    View rootView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        linearLayout = (LinearLayout) rootView.findViewById(R.id.linearlayout);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment1, container, false);
        return rootView;
    }

    public void addPlaces() {
        Button button = new Button(getActivity()); // needs activity context
        button.setText("button name");
        linearLayout.addView(button);
    }
}

这篇关于如何膨胀片段内的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆