首页 > 解决方案 > 构建Andoid浏览器

问题描述

我是 android studio 的新手。我正在构建 android 网络浏览器以供我取乐。在这里,我对添加新标签感到困惑。我使用片段作为标签。我在 mainactivity 布局上创建了一个按钮,当单击该按钮时,它会将新片段添加到片段容器中。

像这样的东西,

public void add_tab()
{
    id++;
    int next_id = id+1;
    String tag = String.valueOf(id);
    Bundle bundle = new Bundle ();
    bundle.putString ("tag",tag);
    bundle.putInt ("id",id);
    bundle.putInt ("next_id",next_id);

    final FrameLayout tab_hosting_frame = new FrameLayout(getApplicationContext ());
    FrameLayout.LayoutParams layoutparams_match_parent_match_parent = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
    tab_hosting_frame.setLayoutParams(layoutparams_match_parent_match_parent);
    tab_hosting_frame.setBackgroundColor (Color.BLUE);
    tab_hosting_frame.setId (id);

    FragmentManager fragment_manager1 = getSupportFragmentManager ();
    New_Tab new_tab_fragment = new New_Tab ();
    FragmentTransaction fr_transaction = fragment_manager1.beginTransaction ();
    fr_transaction.add ((tab_hosting_frame.getId ()),new_tab_fragment,tag);
    fr_transaction.addToBackStack ("Fragment Add");
    //new_tab_fragment.setArguments (bundle);
    new_tab_fragment.setArguments (bundle);
    fr_transaction.commit ();

    tab_hosting_layout.addView (tab_hosting_frame);

    //tab_hosting_layout.setVisibility (View.VISIBLE);

}

我也试过这种方式

public View create_tab()
{
    id++;
    String tag = String.valueOf(id);
    linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View tab_view = new View (getApplicationContext ());
    tab_view = linflater.inflate(R.layout.new_tab_view, null);
    TextView show_id = (TextView)tab_view.findViewById (R.id.tab_id);
    show_id.setText (tag);
    tab_hosting_layout.addView (tab_view);
    return  tab_view;
}

然后在单击按钮时执行,

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);
    tab_hosting_layout = (ConstraintLayout)findViewById (R.id.tab_host);
    add_tab_button = (Button) findViewById (R.id.add_tab_button);
    add_tab_button.setOnClickListener (
                            new View.OnClickListener ()
                            {
                                @Override
                                public void onClick(View v)
                                {
                                    add_tab ();
                                    //or
                                    //create_tab ();
                                }
                            }
                    );

在第二种方法中,当我单击一次后退按钮时,所有添加的选项卡(视图)都会消失一次。不是一步一步来的。。

这个方法好吗??还是有另一种方法或方法可以像在普通网络浏览器中一样创建无限标签?我有点困惑天气我错了还是没有..

如果你们知道帮助我解决任何问题...

标签: javaandroidandroid-fragmentsandroid-tabs

解决方案


推荐阅读