定义一个公共的Layout导航栏,这样在程序中就可以方便的引用了。
效果图:
公共布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#4bc1d2"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_title_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:visibility="gone"
android:background="@drawable/back_selector"
/>
<LinearLayout
android:id="@+id/ll_search_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center" >
<LinearLayout
android:id="@+id/ll_mms_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="@+id/tv_mms_title_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="15sp" />
<TextView
android:id="@+id/tv_mms_title_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_title_popwindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="18sp" />
<ImageView
android:id="@+id/iv_title_downmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/me_header_pulldown"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_title_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:background="@drawable/save_selector"
android:visibility="gone"
/>
</RelativeLayout>
MainActivty.xml:
<FrameLayout 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">
<include
android:id="@+id/in_title"
layout="@layout/me_title" />
</FrameLayout>
package com.example.navigationbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
<span style="white-space:pre"> </span>TextView tv_title;
<span style="white-space:pre"> </span>Button btn_title_left;
<span style="white-space:pre"> </span>Button btn_title_right;
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>setContentView(R.layout.activity_main);
<span style="white-space:pre"> </span>//获取include中布局中的view对象
<span style="white-space:pre"> </span>tv_title = (TextView) findViewById(R.id.tv_title);
<span style="white-space:pre"> </span>btn_title_left =(Button) findViewById(R.id.btn_title_left);
<span style="white-space:pre"> </span>btn_title_right =(Button) findViewById(R.id.btn_title_right);
<span style="white-space:pre"> </span>tv_title.setText("常用号码");
<span style="white-space:pre"> </span>//设置控件为可见
<span style="white-space:pre"> </span>btn_title_left.setVisibility(View.VISIBLE);
<span style="white-space:pre"> </span>btn_title_right.setVisibility(View.VISIBLE);
<span style="white-space:pre"> </span>}
}
源码下载: http://download.csdn.net/detail/qq247300948/7410429