1.自定义导航栏
- 自定义导航栏UI.layout
定义导航栏上有哪些元素以及其布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="44dp">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="17dp"
android:textColor="@color/colorWhite"/>
<TextView
android:id="@+id/back_item_btn"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:background="@mipmap/back_arrow"
android:textColor="@color/colorWhite"
/>
<TextView
android:id="@+id/right_item_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorClear"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:textColor="@color/colorWhite"
android:layout_marginRight="20dp"/>
</RelativeLayout>
- 自定义导航栏.java
1.定义变量引用布局文件上的UI元素
2.定义通用逻辑和响应事件
package mu.navigationbardemo;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.jar.Attributes;
public class NavigationBarLayout extends RelativeLayout {
public TextView titleText;
public TextView backItemBtn;
public TextView rightItemBtn;
public NavigationBarLayout(Context context, AttributeSet attrs){
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.layout_navigation_bar,this);
this.titleText = (TextView) findViewById(R.id.title_text);
this.backItemBtn = (TextView) findViewById(R.id.back_item_btn);
this.rightItemBtn = (TextView) findViewById(R.id.right_item_btn);
backItemBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity) getContext()).finish();
}
});
rightItemBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
在Activity的layout中引用自定义导航栏
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<mu.navigationbardemo.NavigationBarLayout
android:id="@+id/navigation_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
在Activity的java文件中引用自定义导航栏
public class MainActivity extends Activity {
private Button nextBtn;
private NavigationBarLayout navigationBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//隐藏系统原生导航栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
nextBtn = (Button) findViewById(R.id.next_btn);
navigationBar = (NavigationBarLayout) findViewById(R.id.navigation_bar_layout);
//设置导航栏标题
navigationBar.titleText.setText("首页");
//因为是首页,隐藏返回按钮
navigationBar.backItemBtn.setEnabled(false);//返回按钮不可用
navigationBar.backItemBtn.setVisibility(View.GONE);//返回按钮不可见
}
}
隐藏系统导航栏的Activity需要继承自Activity,否则不生效