Android 带图片的自定义标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2086989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom title with image
提问by Alex Volovoy
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.
我可以通过更改 windowXYZStyle 项目通过主题自定义大小、背景图像和文本。
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.
我唯一找不到的东西 - 我如何添加图像而不是文本。我已经尝试requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)
并分配自定义布局 - 但它似乎不起作用。
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>
回答by Dan Lew
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:
此外,您可能需要增加标题的大小;如果你不这样做,那么你的自定义布局的底部可能会被你的 Activity 覆盖。您可以通过添加指定标题大小的主题来更改大小。这将进入值 XML 文件:
<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:
然后您需要在 AndroidManifest.xml 中为您的活动(或应用程序,如果您希望整个应用程序具有此自定义标题栏)设置主题:
<activity android:name=".MyCustomTitleActivity" android:theme="@style/LargeTitleTheme" />
回答by Dave Webb
You could always use:
你总是可以使用:
requestWindowFeature(Window.FEATURE_NO_TITLE);
And then create your own title bar inside your Activity's View.
然后在您的活动视图中创建您自己的标题栏。
回答by Casebash
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 NinePatchimage I would expect that you would be able to achieve a result that would look good on regardless of the device?
您确定需要在标题中使用图像而不是将其作为背景图像的一部分并将标题留空吗?使用NinePatch图像,我希望您能够获得无论使用何种设备都看起来不错的结果?
回答by arunprakashpj
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" />