一般应用界面的顶部都会有一个标题栏,标题栏上会有一个返回键和编辑键。
只需加入2个Button和1个TextView,然后在布局中摆放好就可以了,但是如果每个活动中都编写一遍同样的标题栏代码,就会导致代码的大量重复。
这个时候就可以使用引入布局的方式来解决这个问题。
1.新建一个布局title.xml(编写标题栏)
2.如何在程序中使用这个标题栏,修改activity_main.xml中的代码,添加<include layout="@layout/title"/>(只需要使用include语句将标题栏布局引入)
3.在MainActivity中将系统自带的标题栏隐藏
layout——>title.xml(1)
<LinearLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android : background = "@drawable/black" >
<!--android:background="@drawable/title_bg">-->
<Button
android :id= "@+id/title_back"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_gravity= "center"
android :layout_margin= "5dip"
android :text= "Back"
android :textColor= "#fff" />
<TextView
android :id= "@+id/title_text"
android :layout_width= "0dip"
android :layout_height= "wrap_content"
android :layout_gravity= "center"
android :layout_weight= "1"
android :gravity= "center"
android :text= "Title Text"
android :textColor= "#fff"
android :textSize= "24sp" />
<Button
android :id= "@+id/title_edit"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_gravity= "center"
android :layout_margin= "5dip"
android :text= "Edit"
android :textColor= "#fff" />
<!--android:background="@drawable/back_bg"-->
<!--android:background="@drawable/edit_bg"-->
</LinearLayout>
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android : background = "@drawable/black" >
<!--android:background="@drawable/title_bg">-->
<Button
android :id= "@+id/title_back"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_gravity= "center"
android :layout_margin= "5dip"
android :text= "Back"
android :textColor= "#fff" />
<TextView
android :id= "@+id/title_text"
android :layout_width= "0dip"
android :layout_height= "wrap_content"
android :layout_gravity= "center"
android :layout_weight= "1"
android :gravity= "center"
android :text= "Title Text"
android :textColor= "#fff"
android :textSize= "24sp" />
<Button
android :id= "@+id/title_edit"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_gravity= "center"
android :layout_margin= "5dip"
android :text= "Edit"
android :textColor= "#fff" />
<!--android:background="@drawable/back_bg"-->
<!--android:background="@drawable/edit_bg"-->
</LinearLayout>
layout——>activity_main.xml(2)
<LinearLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<include layout= "@layout/title" />
</LinearLayout>
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<include layout= "@layout/title" />
</LinearLayout>
java——>MainActivity.java(3)
public class MainActivity
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main) ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. activity_main) ;
}
}