问题描述:

I want to use my own title.I think app icon and application name automatically comes while installing the app.I have made my own customized image and i want to use it as title.I have used the below code but it is not working for me

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState)

{

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.homescreen);

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

}

}

titlebar.xml

<?xml version="1.0" encoding="utf-8"?>

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/titlebar"

android:orientation="horizontal" >

<View

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_weight="1" />

</LinearLayout>

网友答案:

Use below code in your activity to remove the default title

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_name);
}

and in your xml create your own custom title using linearlayout having textview and ImageView

网友答案:

You can change it from manifest file also,

Change the appname in res/values/string.xml file. So, the title of your app will change.

Now put the icon in drawable folder which you want to use for your app and

Go to AndroidManifest.xml -> select Application -> you can change icon from here : select your icon from drawable for icon field

网友答案:

you can hide your title bar with the single line

requestWindowFeature(Window.FEATURE_NO_TITLE);

and if you want to customize the the bar or heading with some buttons or functional widgets then customize action bar and use Support action bar for lower api level.

网友答案:

use this string before setContentView:

requestWindowFeature(Window.FEATURE_NO_TITLE);
网友答案:

This is very simple to accomplish

You can change it from manifest file also,

<application
        android:name="com.example.MAPit.Volley.app.AppController"
        android:allowBackup="true"
        android:icon="@drawable/mapit"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

just put the app_name in strings.xml and icon in res/drawable folder.

If you want to change it in code call

setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);

And set the values to whatever you please.

In XML:

<activity android:name=".MyActivity" 
       android:icon="@drawable/my_icon" 
       android:label="My new title" />  

To enable the back button in your app use

 getActionBar().setHomeButtonEnabled(true);
 getActionBar().setDisplayHomeAsUpEnabled(true);

The code should all be placed in your onCreate so that the label/icon changing is transparent to the user, but in reality it can be called anywhere during the activity's lifecycle

网友答案:

You could use jyomin answer or i tried from different answers found on stackoverflow

titlebar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/header_text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:singleLine="true"
        android:text=""
        android:background="@drawable/titlebar"
        android:textSize="7pt"
        android:textStyle="bold"
        android:typeface="sans" />

     <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />


</LinearLayout>


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState)
{
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     //this.requestWindowFeature(Window.FEATURE_NO_TITLE);

     setContentView(R.layout.homescreen);

     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
    // getActionBar().setTitle(R.string.);

}

}

And use this line in manifest which activity tag you are going to use

android:theme="@android:style/Theme"

like this

<activity android:name="com.bar.start.MainActivity"
              android:label="@string/app_name"
               android:screenOrientation="portrait"
                android:theme="@android:style/Theme">
相关阅读:
Top