Android自定义actionbar

<RelativeLayout 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"
    android:background="@drawable/all_bg" >

    <LinearLayout
        android:id="@+id/base_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/action_bar_height"
        android:background="@drawable/actioinbar_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/action_back_image"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/icon_back_bg" />

        <RelativeLayout
            android:id="@+id/actionbar_relativelayout1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:gravity="center" >

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:textColor="@color/result_tts_prompt"
                android:textSize="27sp" />
        </RelativeLayout>

        <TextView
            android:id="@+id/setting_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1"
            android:paddingTop="20dp"
            android:text="设置"
            android:textColor="@color/music_singer"
            android:textSize="30dp"
            android:visibility="gone" />

        <RelativeLayout
            android:layout_height="fill_parent"
            android:layout_width="150dp" >

            <com.mobvoi.incar.ui.views.MicView
                android:id="@+id/micview1"
                android:layout_width="150dp"
                android:layout_centerInParent="true"
                android:layout_height="match_parent" />
        </RelativeLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/base_title" >
    </FrameLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:layout_below="@+id/base_title"
        android:background="@drawable/actionbar_shadow" />

</RelativeLayout>
private ImageView mTitlebarBack;
    private TextView mTitlebarTitle;
    private FrameLayout contentView;
    private LinearLayout mTitlebar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        // init app
        ((CompanionApplication) getApplication()).appInit();

        initView();
    }

    private void initView() {
        mTitlebarBack = (ImageView) findViewById(R.id.base_titlebar_back);
        mTitlebarTitle = (TextView) findViewById(R.id.base_titlebar_title);
        contentView = (FrameLayout) findViewById(R.id.base_content);
        mTitlebar = (LinearLayout) findViewById(R.id.base_titlebar);
        mTitlebarBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        hideTitleBar();
    }
    
    @Override
    public void setContentView(int layoutResId) {
      LayoutInflater.from(this).inflate(layoutResId, contentView);
    }

    @Override
    public void setContentView(View view) {
      contentView.addView(view);
    }

    @Override
    public void setContentView(View view, LayoutParams params) {
      contentView.addView(view, params);
    }
    
    public void hideTitleBar(){
        mTitlebar.setVisibility(View.GONE);
    }
    
    public void showTitleBar(){
        mTitlebar.setVisibility(View.VISIBLE);
    }
    
    public void setTitleBarTitle(String title){
        mTitlebarTitle.setText(title);
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容