仿制iosNavigation自定义标题栏:
首先,创建项目,名为MyNavigationView,建立一个含义hello world 的空界面,然后去掉安卓代码中自带的导航栏:
方法一:在xml中设置不显示系统的导航栏,定义不限社导航栏的格式(style),设置AndroidMainfest的配置,activity注册中表明代码主题(theme)为设置的不显示的格式,代码
在styles.xml代码:
在AndroidMainfest中,代码
android:theme="@style/AppTheme.NoActionBar"
android:name=".MainActivity">
方法二:在Activity的java类中设置:(注意,Activity继承Activity,而不是AppCompatActivity,否则失效,因为AppCompatActivity自带标题栏)
requestWindowFeature(Window.FEATURE_NO_TITLE);
现在界面如下:
现在,实现自定义的标题栏,分析,需要创建一个MyNavigation的java类,和相应的view_navigation.xml文件,为了展示如图效果,还需要资源文件,三张图片。
则:在MainAvtivity中,声明变量navigationView,类型为MyNavigationView,
private MyNaviagtionView naviagtionView ;//声明子空间自定义标题栏
创建view_navigation.xml文件,仿制iosNavigation添加控件,
view_navigation.xml代码如下:
自定义控件含有的子空间可以单独声明一个包含着所有子空间的静态类,代码:
static class NavigationView {
private LinearLayout left ;
private LinearLayout center ;
private LinearLayout right ;
private TextView lefttextView;
private ImageView leftimageView;
private TextView righttextView;
private ImageView rightimageView;
private TextView centertextView;
private ImageView centerimageView;
}
为了方便在MainActivity实现接口,添加点击事件,给控件设置文字和图片,可以使用接口分别实现点击事件,界面文字图片设置,代码:
public interface INaviagtionView {
void leftclick ();
void centerclick ();
void rightclick ();
}
public interface UINavigationView {
String setleftText ();
int setleftImage ();
String setcenterText ();
int setcenterImage();
String setrightText ();
int setrightImage();
}
MyNavigationView.java代码:
public class MyNaviagtionView extends RelativeLayout implements View.OnClickListener {
private INaviagtionView iNaviagtionView ;
private UINavigationView uiNavigationView ;
private NavigationView navigationView ;
public void setiNaviagtionView(INaviagtionView iNaviagtionView) {
this.iNaviagtionView = iNaviagtionView;
}
public void setUiNavigationView(UINavigationView uiNavigationView) {
this.uiNavigationView = uiNavigationView;
//UI界面赋值
if (navigationView != null){
navigationView.lefttextView.setText(uiNavigationView.setleftText());
navigationView.leftimageView.setBackgroundResource(uiNavigationView.setleftImage());
navigationView.centertextView.setText(uiNavigationView.setcenterText());
navigationView.centerimageView.setBackgroundResource(uiNavigationView.setcenterImage());
navigationView.righttextView.setText(uiNavigationView.setrightText());
navigationView.rightimageView.setBackgroundResource(uiNavigationView.setrightImage());
}
}
public MyNaviagtionView(Context context) {
super(context);
initSubViews(context);
}
public MyNaviagtionView(Context context, AttributeSet attrs) {
super(context, attrs);
initSubViews(context);
}
public MyNaviagtionView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initSubViews(context);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyNaviagtionView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initSubViews(context);
}
//MARK : - 子空间添加方法
private void initSubViews(Context context){
View view = LayoutInflater.from(context).inflate(R.layout.view_navigation,this,true);
navigationView = new NavigationView();
navigationView.left = (LinearLayout) view.findViewById(R.id.left);
navigationView.lefttextView = (TextView) view.findViewById(R.id.lefttextview);
navigationView.leftimageView = (ImageView) view.findViewById(R.id.leftimageview);
navigationView.center = (LinearLayout) view.findViewById(R.id.center);
navigationView.centertextView = (TextView) view.findViewById(R.id.centertextview);
navigationView.centerimageView = (ImageView) view.findViewById(R.id.centerimageview);
navigationView.right = (LinearLayout) view.findViewById(R.id.right);
navigationView.righttextView = (TextView) view.findViewById(R.id.righttextview);
navigationView.rightimageView = (ImageView) view.findViewById(R.id.rightimageview);
navigationView.left.setOnClickListener(this);
navigationView.center.setOnClickListener(this);
navigationView.right.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (iNaviagtionView == null )
return;
switch (v.getId()) {
case R.id.left:
iNaviagtionView.leftclick();
break;
case R.id.center:
iNaviagtionView.centerclick();
break;
case R.id.right:
iNaviagtionView.rightclick();
break;
default:
break;
}
}
}
public class MainActivity extends AppCompatActivity implements MyNaviagtionView.INaviagtionView , MyNaviagtionView.UINavigationView{
private MyNaviagtionView naviagtionView ;//声明子空间自定义标题栏
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
naviagtionView = (MyNaviagtionView) findViewById(R.id.mynavigation);
naviagtionView.setiNaviagtionView(this);
naviagtionView.setUiNavigationView(this);
}
//MARK : - 实现点击事件
@Override
public void leftclick() {
Toast.makeText(this,"left clcik",Toast.LENGTH_LONG).show();
}
@Override
public void centerclick() {
Toast.makeText(this,"center clcik",Toast.LENGTH_LONG).show();
}
@Override
public void rightclick() {
Toast.makeText(this,"right clcik",Toast.LENGTH_LONG).show();
}
//实现界面设置
@Override
public String setleftText() {
return "文件";
}
@Override
public int setleftImage() {
return R.mipmap.wenjianlight;
}
@Override
public String setcenterText() {
return "NavigationView";
}
@Override
public int setcenterImage() {
return R.mipmap.yunlight;
}
@Override
public String setrightText() {
return "更多";
}
@Override
public int setrightImage() {
return R.mipmap.textlight;
}
}
需要源码可以到我的资源中下载AndroidNavigationView,地址:
http://download.csdn.net/detail/flytosky_l/9769717