android actionbar 自定义布局,ActionBar 自定义布局定义

本文介绍了如何在Android应用中自定义ActionBar的布局。通过在Activity中设置显示自定义布局并加载指定的XML文件,实现包括图标、输入框和搜索按钮在内的组件展示。详细步骤包括在onCreate()方法中调用自定义方法,以及定义XML布局文件的结构和属性。
摘要由CSDN通过智能技术生成

在Activity中加载ActionBar的自定义布局

(1)ActionBar加载自定义布局的代码封装如下:

private void setActionBarLayout(int layoutId) {

// TODO Auto-generated method stub

ActionBar actionBar = getActionBar();

if(null != actionBar){

actionBar.setDisplayShowHomeEnabled(false);

actionBar.setDisplayShowCustomEnabled(true);

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View v = inflater.inflate(layoutId, null);

ActionBar.LayoutParams lParams = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

actionBar.setCustomView(v, lParams);

}

}

(2)在onCreate()方法中调用该方法,并传入自定义的布局文件ID:

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setActionBarLayout(R.layout.actionbar_layout);

}

(3)自定义布局文件“actionbar_port_layout.xml ”代码如下:

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="44dp"

android:orientation="horizontal"

android:gravity="center_vertical"

android:background="#008000">

android:layout_marginLeft="24dp"

android:layout_width="36dp"

android:layout_height="36dp"

android:src="@drawable/ic_launcher"/>

android:layout_marginLeft="24dp"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="36dp"

android:hint="Please input "

android:id="@+id/et_input"/>

android:layout_marginRight="20dp"

android:layout_marginLeft="20dp"

android:layout_width="30dp"

android:layout_height="30dp"

android:src="@drawable/action_search"/>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值