只要把下面的代碼在你的TabHost
主要活動 -
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
ImageView image = (ImageView)findViewById(R.id.header);
image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(CustomWindowTitle.this, "This is sample", Toast.LENGTH_SHORT).show();
return false;
}
});
}
Window_title.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#323331">
<ImageView
android:id="@+id/header"
android:src="@drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
希望這能對你有所幫助。見下面我的輸出 -
更新
我知道了。你宣稱圖像的OnclickListener
後您的自定義標題請像下面在你的代碼的變化 -
public class MyCustomTab2Activity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//Tab Content
setContentView(R.layout.my_tab_home);
//custom title bar content
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.logintitle);
ImageView imag = (ImageView) findViewById(R.id.iv_logout);
imag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
......
...........
}
}
希望這有助於你。
是的。我需要做.. –
我試過你的代碼。我也收到了空指針異常。試試我更新的答案。是否有用? – Praveenkumar