带图像的自定义标题
我正在通过禁用标准标题并自己管理所有内容来为活动创建自定义标题。我想知道是否可以根据我的需要替换/主题标准标题。
我可以通过更改 windowXYZStyle 项目来通过主题自定义大小、背景图像和文本。
我唯一找不到的东西 - 如何添加图像而不是文本。 我尝试过 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)
并分配自定义布局 - 但它似乎不起作用。
编辑:这是测试建议的报告,代码如下 - 结果 - 图像视图未显示。
活动
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以设置您自己的自定义标题布局,但执行顺序很重要。您必须按以下顺序执行操作:
此外,您可能需要增加标题的大小;如果不这样做,那么自定义布局的底部可能会被您的活动覆盖。您可以通过添加指定标题大小的主题来更改大小。这将进入一个值 XML 文件:
然后您需要在 AndroidManifest.xml 中为您的 Activity(或应用程序,如果您希望整个应用程序具有此自定义标题栏)设置主题:
It's possible to set your own custom title layout, however the order of execution matters. You must do things in this order:
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:
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:
您始终可以使用:
然后在活动视图中创建自己的标题栏。
You could always use:
And then create your own title bar inside your Activity's View.
您确定需要在标题中使用图像,而不是仅仅将其组合为背景图像的一部分并将标题留空吗?使用 NinePatch 图像我希望你能够实现无论设备如何,结果都会很好看?
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?
我使用以下代码使自定义标题适合屏幕
I have used the following code to fit my custom title to the screen