cch*_*son 186
这个主题将帮助您开始在xml文件中构建自己的标题栏并在活动中使用它
编辑
以下是上述链接内容的简短摘要 - 这只是设置文本的颜色和标题栏的背景 - 没有调整大小,没有按钮,只是最简单的样本
res/layout/mytitle.xml - 这是代表标题栏的视图
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
Run Code Online (Sandbox Code Playgroud)
res/values/themes.xml - 我们希望保留默认的android主题,只需要更改标题背景的背景颜色.因此,我们创建一个继承默认主题的主题,并将背景样式设置为我们自己的样式.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
res/values/styles.xml - 这是我们设置主题以使用我们想要的标题背景颜色的地方
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">@color/titlebackgroundcolor</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
res/values/colors.xml - 在此处设置所需的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
在AndroidMANIFEST.xml中,在应用程序(对于整个应用程序)或活动(仅对此活动)标记中设置主题属性
<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...
Run Code Online (Sandbox Code Playgroud)
从Activity(名为CustomTitleBar):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
}
Run Code Online (Sandbox Code Playgroud)
Sep*_*phy 18
感谢这个明确的解释,但是我想通过询问一个链接的问题来增加你的答案(不要真的想做一个新的帖子,因为这个是我问题的基础).
我在超类中声明我的标题栏,我的所有其他活动都是孩子,必须只更改一次栏的颜色.我还想添加一个图标并更改栏中的文字.我做了一些测试,并设法改变其中一个或另一个,但不是同时改变两个(使用setFeatureDrawable和setTitle).理想的解决方案当然是遵循链接中给出的线程中的解释,但是当我在超类中声明时,由于setContentView和R.id.myCustomBar中的布局,我有一个问题,因为如果我记得好,我只能调用一次setContentView ...
编辑 找到我的答案:
对于那些像我一样喜欢使用超类的人,因为它非常适合在应用程序中随处可用菜单,它在这里工作原理相同.
只需将其添加到您的超类:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.customtitlebar);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
customTitleText = (TextView) findViewById(R.id.customtitlebar);
Run Code Online (Sandbox Code Playgroud)
(您必须将textview声明为受保护的类变量)
然后,这就是你的应用程序的所有功能(例如,如果你所有的活动都是这个类的孩子),你只需要打电话
customTitleText.setText("Whatever you want in title");
Run Code Online (Sandbox Code Playgroud)
并且您的标题栏将被编辑.
在我的案例中关联的XML是(R.layout.customtitlebar):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/background">
<ImageView android:layout_width="25px" android:layout_height="25px"
android:src="@drawable/icontitlebar"></ImageView>
<TextView android:id="@+id/customtitlebar"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:text="" android:textColor="@color/textcolor" android:textStyle="bold"
android:background="@color/background" android:padding="3px" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
mik*_*ate 11
还有另一种方法可以更改背景颜色,但如果Window的View层次结构及其标题发生更改,则它可能会在未来的Android版本上失败.但是,在这种情况下,代码不会崩溃,只是错过设置所需的颜色.
在您的活动中,例如onCreate,执行:
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.rgb(0x88, 0x33, 0x33));
}
}
Run Code Online (Sandbox Code Playgroud)
此代码有助于在Android中以编程方式更改标题栏的背景.将颜色更改为您想要的任何颜色.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1c2833")));
}
Run Code Online (Sandbox Code Playgroud)
小智 6
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
ViewParent parent = titleView.getParent();
if (parent != null && (parent instanceof View)) {
View parentView = (View)parent;
parentView.setBackgroundColor(Color.RED);
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,您可以尝试使用title而不是titlebar, 这将影响应用程序中的所有活动