Custom title with image

孤街浪徒 提交于 2019-11-26 09:20:46

问题


i\'m creating custom title for activity by disabling standard one and managing everything myself. I wonder if it\'s possible to replace/theme standart title to my needs.

I can customize size, background image, and text via themes by changing windowXYZStyle items.

The only thing i couldn\'t find - how i can add image instead of text. I\'ve tried requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) and assign custom layout - but it doesn\'t seems to work.

EDIT : Here is a report of testing suggestions, code is below - result - image view is not showing up.

Activity

public class SettingsActivity extends PreferenceActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

    }
}

XML :

<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:id=\"@+id/title\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"26dip\"
    android:paddingLeft=\"5dip\"
    android:background=\"@drawable/titlebar_bg\"
    android:layout_gravity=\"left|center\"
>
    <ImageView
        android:id=\"@+id/logo\"
        android:src=\"@drawable/title_logo\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\" />

</LinearLayout>

回答1:


It's possible to set your own custom title layout, however the order of execution matters. You must do things in this order:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_title);

Additionally, you may need to increase the size of the title; if you don't, then the bottom of your custom layout may just be covered up by your Activity. You can change the size by adding a theme that specifies the title's size. This would go into a values XML file:

<resources>
    <style name="LargeTitleTheme">
        <item name="android:windowTitleSize">40dip</item>
    </style>
</resources>

Then you'd need to set the theme for your Activity (or Application, if you want the entire application to have this custom title bar) in AndroidManifest.xml:

<activity android:name=".MyCustomTitleActivity" android:theme="@style/LargeTitleTheme" />



回答2:


You could always use:

requestWindowFeature(Window.FEATURE_NO_TITLE);

And then create your own title bar inside your Activity's View.




回答3:


Are you sure that you need to use an image in the title rather than just combining it as part of your background image and leaving the title blank? Using a NinePatch image I would expect that you would be able to achieve a result that would look good on regardless of the device?




回答4:


I have used the following code to fit my custom title to the screen

<ImageView
    android:id="@+id/header_img"
    android:src="@drawable/header_img"
    android:layout_width="700dip"
    android:layout_height="70dip" />


来源:https://stackoverflow.com/questions/2086989/custom-title-with-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!