引入布局

一般应用界面的顶部都会有一个标题栏,标题栏上会有一个返回键和编辑键。
只需加入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>
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>
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) ;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值