自定义actionBar布局:标题居中,左边有返回按键
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp" > <ImageView
android:id="@+id/home"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="10dp"
android:clickable="true"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/wd007" /> <TextView
android:id="@+id/titlebar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="消 息"
android:textColor="@color/titlecolor"
android:textSize="20sp" /> <ImageView
android:id="@+id/right"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="invisible"
android:src="@drawable/wd007" /> </RelativeLayout>
java文件里面使用:
package com.tuyoung.wu; import com.example.toyoung.R; import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView; public class Message extends Activity{
private ImageView back; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
setCustomActionBar();
} private void setCustomActionBar() {
ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
View mActionBarView = LayoutInflater.from(this).inflate(R.layout.customtitle, null);
ActionBar actionBar = getActionBar();
actionBar.setCustomView(mActionBarView, lp);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false); back = (ImageView) mActionBarView.findViewById(R.id.home);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
}); } }
分割间距:
<ImageView
android1:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff"
/>