Android LinearLayout加载Fragment

为LinearLayout添加Fragment效果

1、创建Fragment

public class Fragment1 extends Fragment {
    public static Fragment1 getInstance(Bundle bundle) {
        Fragment1 fg = new Fragment1();
        fg.setArguments(bundle);
        return fg;
    }
    protected View contentView;

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

    }
    public void initViews(){

        TextView textView =(TextView)contentView.findViewById(R.id.f1te);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(),"ssssf1",Toast.LENGTH_SHORT).show();
            }
        });
    }

}

附activity_main.xml

<LinearLayout
    android:id="@+id/music_control"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:padding="5dp"
    android:orientation="horizontal"
    android:background="@color/grey">
</LinearLayout>

2、添加方法

/**
 * 用于设置底部的控制音乐播放的fragment
 */
private void setBottomController() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.add(R.id.music_control,new Fragment1());
    transaction.commit();
}
发布了339 篇原创文章 · 获赞 66 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/93976805
今日推荐