Implement custom title
App with custom title |
Create /res/layout/custom_title.xml, to define the layout of your custom title bar.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/titletext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="italic|bold"
android:text="http://android-coding.blogspot.com/" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
</LinearLayout>
MainActivity.java
package com.example.androidcustomtitle;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
if(customTitleSupported){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
Toast.makeText(getApplicationContext(),
"Custom Title Applied",
Toast.LENGTH_LONG).show();
//Set title text programmatically
TextView titleText = (TextView)findViewById(R.id.titletext);
titleText.setText("Android-Coding");
}else{
Toast.makeText(getApplicationContext(),
"FEATURE_CUSTOM_TITLE not supported!",
Toast.LENGTH_LONG).show();
}
}
}
Also modify AndroidManifest.xml to use style without title features on android:theme; otherwise "android.util.AndroidRuntimeException: You cannot combine custom titles with other title features" will be thrown. "@android:style/Theme.Light" and "@android:style/Theme.Black" have been tested work ok.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidcustomtitle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light" >
<activity
android:name="com.example.androidcustomtitle.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
0 komentar:
Posting Komentar