【1】 ActionBar最顶端的自定义

<pre name="code" class="java"><span style="font-size:18px;">public class MainActivity extends AppCompatActivity {
    ImageButton imagebar;
    TextView textbar;
    ActionBar supportActionBar;
    protected ViewPager viewpager;
    protected TabLayout tablayout;
    private FragmentViewPageradapter fragmentViewPageradapter;

    String[]daohang={"首页","游戏","娱乐","女神","我的"};
    int [] image={R.drawable.shouye,R.drawable.youxi,
            R.drawable.yule,R.drawable.nvshen,R.drawable.wode};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
       //最顶部actionbar设置
        supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayShowCustomEnabled(true);
        supportActionBar.setCustomView(R.layout.actionbar);//根据自己需要自定义布局
        textbar= (TextView) findViewById(R.id.actiongbar);
        imagebar= (ImageButton) findViewById(R.id.imagebutton);


        initView();
    }
//此ActionBar是在Tablayout结合写的,下半部分代码如下
//Tablayout监听事件 用其中方法对顶部修改
tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
//获得Tab 点击变化时的下表 根据下表修稿不同的ActionBar
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        switch (position){
            case 0:
               textbar.setText("首页");
                break;
            case 1:
                textbar.setText("游戏");
                break;
            case 2:
                textbar.setText("娱乐");
                break;
            case 3:
                textbar.setText("女神");
                break;
            case 4:
                textbar.setText("我的");
                break;
        }
    }
</span>
//******************************
 
<span style="font-size:18px;">//布局文件如下</span>
<span style="font-size:18px;"></span><pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#FAFAFA"
    android:layout_height="48dp">
<TextView
    android:id="@+id/actiongbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#2B2B2B"
    android:textSize="25sp"
    android:text="首页"
   />
   <ImageButton
        android:background="#FAFAFA"
        android:id="@+id/imagebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/bar_find" />
</RelativeLayout>

 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值