【1】 ActionBar最顶端的自定义

来源:互联网 发布:精通 d3.js 第2版 pdf 编辑:程序博客网 时间:2024/04/29 12:29
<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>

                                             
0 0
原创粉丝点击