Android:如何自定义标题栏

觉得Android自动生成的标题栏太丑,不够大? 想要做和Android流行app那样的好看的还带按键的标题栏?

自定义标题栏其实很简单。 首先创建一个XML文件, 取一个合适的名字(此处为title_main)。然后, 将自己想要的标题栏布局写上去, 例如

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title_layout_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:layout_alignParentTop="true"
    android:weightSum="10">
      
      <EditText 
        android:id="@+id/search_box"
        android:layout_width="0dp"
        android:layout_weight="8"
        android:layout_height="match_parent"
        android:drawableLeft="@drawable/ic_action_search"
        android:textSize="25sp"
        android:hint="search"/>"
      
      <Button
	android:id="@+id/go"
	android:layout_width="0dp"
	android:layout_weight="2"
	android:layout_height="match_parent"
	android:text = "GO"/>  

</LinearLayout>

接着在activity的onCreate()方法中加入以下代码

	requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

	setContentView(R.layout.activity_main);

	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_main);

提示:这三行代码需要按顺序写


接下来,有的人可能会遇到问题,如何设置标题栏的高度, 使得它能够很好的呈现里面的内容

首先要在res\values\的style.xml文件中写入如下代码,新建一种style

<?xmlversion="1.0" encoding="utf-8"?>
<resources>
         <style name="titlestyle"parent="android:Theme.Light">
                   <item name="Android:windowTitleSize">38dip</item> //标题栏的高度
         </style>
</resources>
这样,就基本能够实现标题栏的自定义了


其实还有一种方法就是在activity的onCreate方法中调用

requestWindowFeature(Window.FEATURE_NO_TITLE);

隐藏标题栏

然后,在布局XML文件中加入一个置顶的自定义Layout ;)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值