安卓 自定义顶部标题栏

思路及实现步骤

1.定义标题栏布局 
2.自定义TitleActivity控制标题栏按钮监听 
3.在TitleActivity中实现标题栏以下内容切换

效果如下: 
  

  • 首先定义标题栏

layout_title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_titlebar"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:background="#ed4255">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="marquee"
        android:gravity="center_horizontal|center"
        android:singleLine="true"
        android:text="标题栏"
        android:textColor="#ffffffff"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button_backward"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:background="@drawable/title_button_selector"
        android:drawableLeft="@drawable/back_arrow"
        android:drawablePadding="6dp"
        android:ellipsize="end"
        android:gravity="center"
        android:onClick="onClick"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:text="返回"
        android:textColor="#ffffffff"
        android:textSize="18dp"
        android:visibility="invisible" />

    <Button
        android:id="@+id/button_forward"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@drawable/title_button_selector"
        android:drawablePadding="6dp"
        android:ellipsize="end"
        android:gravity="center"
        android:onClick="onClick"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:text="提交"
        android:textColor="#ffffffff"
        android:textSize="18dp"
        android:visibility="invisible" />

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 定义界面的标题栏和标题栏以下内容的布局 
    此处使用<include>标签引入标题栏,且下方有定义一个空的FrameLayout的布局。

activity_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/layout_title" />

    <FrameLayout
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#fff"/>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 定义TitleActivity布局以及按钮

TitleActivity.java

public class TitleActivity extends Activity implements View.OnClickListener {

    //private RelativeLayout mLayoutTitleBar;
    private TextView mTitleTextView;
    private Button mBackwardbButton;
    private Button mForwardButton;
    private FrameLayout mContentLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupViews();   //加载 activity_title 布局 ,并获取标题及两侧按钮
    }


    private void setupViews() {
        super.setContentView(R.layout.activity_title);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
        mBackwardbButton = (Button) findViewById(R.id.button_backward);
        mForwardButton = (Button) findViewById(R.id.button_forward);
    }

    /**
     * 是否显示返回按钮
     *
     * @param backwardResid 文字
     * @param show          true则显示
     */
    protected void showBackwardView(int backwardResid, boolean show) {
        if (mBackwardbButton != null) {
            if (show) {
                mBackwardbButton.setText(backwardResid);
                mBackwardbButton.setVisibility(View.VISIBLE);
            } else {
                mBackwardbButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 提供是否显示提交按钮
     *
     * @param forwardResId 文字
     * @param show         true则显示
     */
    protected void showForwardView(int forwardResId, boolean show) {
        if (mForwardButton != null) {
            if (show) {
                mForwardButton.setVisibility(View.VISIBLE);
                mForwardButton.setText(forwardResId);
            } else {
                mForwardButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 返回按钮点击后触发
     *
     * @param backwardView
     */
    protected void onBackward(View backwardView) {
        Toast.makeText(this, "点击返回,可在此处调用finish()", Toast.LENGTH_LONG).show();
        //finish();
    }

    /**
     * 提交按钮点击后触发
     *
     * @param forwardView
     */
    protected void onForward(View forwardView) {
        Toast.makeText(this, "点击提交", Toast.LENGTH_LONG).show();
    }


    //设置标题内容
    @Override
    public void setTitle(int titleId) {
        mTitleTextView.setText(titleId);
    }

    //设置标题内容
    @Override
    public void setTitle(CharSequence title) {
        mTitleTextView.setText(title);
    }

    //设置标题文字颜色
    @Override
    public void setTitleColor(int textColor) {
        mTitleTextView.setTextColor(textColor);
    }


    //取出FrameLayout并调用父类removeAllViews()方法
    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);
        onContentChanged();
    }

    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }


    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     * 按钮点击调用的方法
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button_backward:
                onBackward(v);
                break;

            case R.id.button_forward:
                onForward(v);
                break;

            default:
                break;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • MainActivity中调用时直接 extends TitleActivity,使用在TitleActivity中定义的方法
public class MainActivity extends TitleActivity {

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

        setTitle("标题栏");
        showBackwardView(R.string.text_back,true);
        showForwardView(R.string.text_forward,true);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Studio中编写顶部标题栏可以通过以下步骤实现: 1. 在XML布局文件中添加一个`Toolbar`控件,用于显示标题栏。例如,可以在`activity_main.xml`文件中添加如下代码: ```xml <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:title="My Title" android:titleTextColor="@android:color/white" /> ``` 2. 在Activity的Java代码中找到该`Toolbar`控件,并将其设置为Activity的ActionBar。例如,在`MainActivity.java`文件的`onCreate()`方法中添加如下代码: ```java Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); ``` 3. 如果需要自定义标题栏的样式,可以在`styles.xml`文件中定义一个主题,并将其应用到Activity上。例如,在`styles.xml`文件中添加如下代码: ```xml <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- 自定义标题栏样式 --> <item name="android:actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="Widget.AppCompat.ActionBar"> <!-- 自定义标题栏背景颜色 --> <item name="android:background">@color/colorPrimary</item> <!-- 自定义标题栏文字颜色 --> <item name="android:titleTextStyle">@style/MyActionBarTitle</item> </style> <style name="MyActionBarTitle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title"> <!-- 自定义标题栏文字颜色 --> <item name="android:textColor">@android:color/white</item> </style> ``` 然后,在`AndroidManifest.xml`文件中将该主题应用到对应的Activity上。例如: ```xml <activity android:name=".MainActivity" android:theme="@style/AppTheme" /> ``` 这样就可以在Android Studio中编写顶部标题栏了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值