android Toolbar在Fragment中的应用

当我们使用TAB切换画面时,Fragment是其中的一种方法。
首先建立画面的XML文件,文件名tab1.xml。如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="ExtraText">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ToolBarPopTheme"
        app:titleTextAppearance="@style/ToolbarTitle" />

    <SearchView
        android:id="@+id/search_view1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/searchView"
        android:queryBackground="@null"
        android:queryHint="请输入查询名称!"
        app:layout_constraintEnd_toStartOf="@+id/ll1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar2"
        tools:ignore="MissingConstraints" />


    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"

        android:background="@color/color_text_style1"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="@+id/search_view1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/search_view1"
        app:layout_constraintTop_toTopOf="@+id/search_view1"
        tools:ignore="MissingConstraints">

        <TextView
            android:id="@+id/dx_show"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:gravity="center"
            android:text="全部"
            android:textColor="@color/colordl"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/reg_count"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:background="@drawable/text_style1"
            android:gravity="center"
            android:textColor="@color/colordl" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:textSize="25sp"
        android:background="@drawable/text_frame"
        app:layout_constraintHorizontal_weight="1"
        android:gravity="center"
        android:text="地址"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_view1"
        tools:ignore="MissingConstraints,UnknownId"
        tools:layout_editor_absoluteY="106dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="值"
        android:textSize="25sp"
        android:background="@drawable/text_frame"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/search_view1"
        tools:ignore="MissingConstraints" />

    <ListView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        tools:ignore="MissingConstraints"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

实现画面如下:
在这里插入图片描述
建立对应的fragmnet代码,如下:

public class tab1 extends Fragment {
   private SearchView searchView;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.tab1,container,false);
        androidx.appcompat.widget.Toolbar toolbar =(Toolbar)view.findViewById(R.id.toolbar2);
        toolbar.setTitle("设备");
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);

        //Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);//隐藏默认的Title

        // 以下动作让标题居中显地
        TextView textView = (TextView) toolbar.getChildAt(0);//主标题
        textView.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;//填充父类
        textView.setGravity(Gravity.CENTER);

        searchView = (SearchView) view.findViewById(R.id.search_view1);
        searchView.setIconifiedByDefault(false);
        searchView.setSubmitButtonEnabled(true);//增加提交按钮
        // TextView textView1=searchView.findViewById(android.support.
        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text",null,null);
//获取到searchview TextView的控件
        TextView textView1 = (TextView) searchView.findViewById(id);
//设置字体大小为14sp
        textView1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);//14sp


        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.action_exit:
                        ActivityCollector.killAllActivity();
                        break;
                    case R.id.action_test:
                        //Intent intent=new Intent(Main4Activity.this,Main3Activity.class);
                        //startActivity(intent);
                        //overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                }
                return true;
            }
        });



        return view;
    }


}

其中
通过ID查找到布局文件

View view=inflater.inflate(R.layout.tab1,container,false);

通过布局文件找到Toolbar

androidx.appcompat.widget.Toolbar toolbar =(Toolbar)view.findViewById(R.id.toolbar2);

设置Toolbar标题

toolbar.setTitle("设备");

以下两行代码一次要添加,并且要在toolbar.setTitle(“设备”)之后添加,如果写在前面标题名就变成Activity的了。

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
setHasOptionsMenu(true);

标题居中设置

TextView textView = (TextView) toolbar.getChildAt(0);//主标题
        textView.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;//填充父类
        textView.setGravity(Gravity.CENTER);

在往下的代码是设置SearchView控件和设置事件监听,就不详细说明了。可自行百度,因为我也是百度出来的。

主代码:添加tab,自定义样式

//采用自定义样式设定标签
        /*添加tab*/
        for (int i = 0; i < 3; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.tabview, null, false);
            TextView tabviewtextView = (TextView) view.findViewById(R.id.tabveiw_text);
            ImageView imageView = (ImageView) view.findViewById(R.id.tabview_icon);
            TextView tabviewtextView1=(TextView)view.findViewById(R.id.icontext);
            Intent intent=new Intent(Main3Activity.this,Main4Activity.class);
            //tabHost.setup(this.getLocalActivityManager());


            switch (i) {
                case 0:
                    tabviewtextView.setText("寄存器");
                    tabviewtextView1.setVisibility(View.GONE);
                    imageView.setImageResource(R.drawable.reg1);
                    tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(view).setContent(R.id.tab1));
                    break;
                case 1:
                    tabviewtextView.setText("报表");
                    tabviewtextView1.setVisibility(View.GONE);
                    imageView.setImageResource(R.drawable.report1);
                    tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(view).setContent(R.id.tab2));
                    break;
                case 2:
                    tabviewtextView.setText("报警");
                    imageView.setImageResource(R.drawable.alarm1);
                    tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator(view).setContent(R.id.tab3));
                    break;

            }



        }

通过tab的切换切换Fragment

 FragmentManager fragmentManager=getSupportFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                switch (tabId){

                    case "tab1":
                        ((ImageView)tabHost.getCurrentTabView().findViewById(R.id.tabview_icon)).setImageResource(R.drawable.reg);

                        tab1 tab1fragment = new tab1();
                        fragmentTransaction.replace(R.id.fram,tab1fragment,"tag1");
                        fragmentTransaction.commit();

                        break;
                    case "tab2":
                        ((ImageView)tabHost.getCurrentTabView().findViewById(R.id.tabview_icon)).setImageResource(R.drawable.report);
                       tab2 tab2fragment = new tab2();
                       fragmentTransaction.replace(R.id.fram,tab2fragment,"tag2");
                       fragmentTransaction.commit();
                        break;
                    case "tab3":
                        ((ImageView)tabHost.getCurrentTabView().findViewById(R.id.tabview_icon)).setImageResource(R.drawable.alarm);
                        tab3 tab3fragment = new tab3();
                        fragmentTransaction.replace(R.id.fram,tab3fragment,"tag3");
                        fragmentTransaction.commit();
                        break;

                }
            }
        });

.这篇文章主要是用来记录学习过程中的一些知识点,方便以后查找学习。

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读