安卓开发学习14:Action Bar

解析

在这里插入图片描述
在这里插入图片描述

显示和隐藏Action Bar

全局控制Action Bar

API11以上默认是有Action Bar
在这里插入图片描述
所以在build gradle中配置最小sdk大于11就会默认有Action Bar
在这里插入图片描述
对于不显示Action Bar,只需要设置theme带.NoActionBar的主题
在这里插入图片描述

局部Action Bar设置

在这里插入图片描述

动态Action Bar配置

以上是在配置文件中直接配置的,不够灵活,以下通过代码实现
1、MainActivy.java

package com.example.login_status_page;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    ActionBar actionBar = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button show = findViewById(R.id.show);
        Button hide = findViewById(R.id.hide);
        actionBar = getSupportActionBar();
        show.setOnClickListener(l);
        hide.setOnClickListener(l);
    }
    View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.show:
                    actionBar.show();
                    break;
                case R.id.hide:
                    actionBar.hide();
                    break;
            }
        }
    };
}

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示"
        ></Button>

    <Button
        android:id="@+id/hide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隐藏"
        ></Button>


</LinearLayout>

添加Action Item

图标显示和溢出Action Bar

这是没有放到溢出Action bar的Action item
在这里插入图片描述
右图为溢出的Action item
在这里插入图片描述

配置Action Item

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1、action_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search"
        android:icon="@drawable/search"
        android:title="搜索"
        android:showAsAction="always"
        app:showAsAction="always">
    </item>

    <item
        android:id="@+id/msg"
        android:icon="@drawable/msg"
        android:title="消息"
        android:showAsAction="ifRoom|withText">
    </item>

    <item
        android:id="@+id/about"
        android:icon="@drawable/search"
        android:title="关于"
        app:showAsAction="never">
    </item>
</menu>

always:就是显示到Action bar上,就像上边的图标
ifRoom:如果Action bar够位置,那就显示图标到上边
withText:携带文字
ifRoom|withTex:就上上述的组合,效果就是上边的
nerver:只放到溢出空间中
在这里插入图片描述

2、MainAvtivity.java

package com.example.custom_actionbar_page;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }
//  装载Action Item到Activity中
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater  = new MenuInflater(MainActivity.this);
        menuInflater.inflate(R.menu.action_bar, menu);
        return super.onCreateOptionsMenu(menu);
    }
//  监听Action Item点击
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

}

添加Action View

解析

通过Action View可以定义更丰富的Action bar显示,而不局限于显示图标。从名字就知道,就是可将其他布局文件加载到Action bar中
在这里插入图片描述

app:actionViewClass配置

配置app:actionViewClass,设置类文件进行配置Action view,android.widget.SearchView是安卓系统内置的搜索组件

    <item
        android:id="@+id/search"
        android:icon="@drawable/search"
        android:title="搜索"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always"
        android:showAsAction="always"
        ></item>

直接导入布局文件

通过app:actionLayout添加布局文件

    <item
        android:id="@+id/msg"
        android:icon="@drawable/msg"
        android:title="搜索"
        app:actionLayout="@layout/actionitem_msg"
        android:actionLayout="@layout/actionitem_msg"
        app:showAsAction="always"
        android:showAsAction="always"
        ></item>

Action Bar和Tab

在这里插入图片描述

实例

1、实现以下顶部标题栏,被配置每个tab所对应的fragment
在这里插入图片描述
2、activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
   
</LinearLayout>

3、fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment1" />

</FrameLayout>

4、fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".Fragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment2" />

</FrameLayout>

5、Fragment1.java

package com.example.action_tab_page;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment1#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment1 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Fragment1() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment1 newInstance(String param1, String param2) {
        Fragment1 fragment = new Fragment1();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_1, container, false);
    }
}

6、Fragment2.java

package com.example.action_tab_page;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Fragment2#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Fragment2 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public Fragment2() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Fragment2.
     */
    // TODO: Rename and change types and number of parameters
    public static Fragment2 newInstance(String param1, String param2) {
        Fragment2 fragment = new Fragment2();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_2, container, false);
    }
}

7、MyTabListener.java

package com.example.action_tab_page;

import android.app.Activity;

import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class MyTabListener implements ActionBar.TabListener {

    private Activity context;  // 指定的上下文
    private Class aClass = null; // 指定fragment所对应的class
    private Fragment fragment; // 指定fragment

    public MyTabListener() {
    }

    public MyTabListener(Activity ctx, Class clas){
        this.context = ctx;
        aClass = clas;
    }

    // tab被点击时候
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        // 如果fragment为空那么就按照参数新建一个对应的实例
        if(fragment == null){
            fragment = Fragment.instantiate(context, aClass.getName());
            ft.add(android.R.id.content, fragment);
        }

     


    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        // 当离开tab就卸载fagement
        if(fragment != null){
            ft.detach(fragment);
        }
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

    }
}

8、MainActivity.java

package com.example.action_tab_page;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
//      设置Action Bar为多tab模式
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//      为给tab足够空间,这里设置为无标题栏
        actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
//      设置tab文本内容以及配置对应的fragment
        actionBar.addTab(actionBar.newTab().setText("音乐").setTabListener(new MyTabListener(MainActivity.this, Fragment1.class)));
        actionBar.addTab(actionBar.newTab().setText("游戏").setTabListener(new MyTabListener(MainActivity.this, Fragment2.class)));

    }
}

层级式导航

在这里插入图片描述

实例

1、需求:实现跳转后回退:MainActivity跳到MainActivity2
在这里插入图片描述
2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/jump"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"
        ></Button>
</LinearLayout>

2、activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

</LinearLayout>

3、MainActivity.java

package com.example.action_tab_page;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button jump = findViewById(R.id.jump);

        jump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//              页面跳转
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);
            }
        });
    }
}

4、MainActivity2.java

package com.example.action_tab_page;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NavUtils;

import android.os.Bundle;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
//      判断是否单签页面是否存在父Activity
        if(NavUtils.getParentActivityName(MainActivity2.this) != null){
            // 设置向左的箭头
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}

5、在AndroidManifest.xml中进行配置:在MainActivity2.java上进行配置meta,指定其父Activity

        <activity
            android:name=".MainActivity2"
            android:exported="false"
            >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"
                ></meta-data>

        </activity>>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值