如何在android中的活动屏幕上添加一个额外的标题栏?

时间:2011-03-05 08:23:22

标签: android android-layout

除了Android中已有的标题栏外,我如何在屏幕上添加标题栏?我还想为这个标题栏选择一种颜色。

你能告诉我这是怎么做到的吗?

2 个答案:

答案 0 :(得分:0)

这个thread将帮助您开始在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"
   />

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>

res / values / styles.xml - 这是我们设置主题以使用我们想要的标题背景颜色的地方

<?xml version="1.0" encoding="utf-8"?>
<resources> 
   <style name="WindowTitleBackground">     
        <item name="android:background">@color/titlebackgroundcolor</item>                   
    </style>
</resources>

res / values / colors.xml - 在此处设置您想要的颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>
</resources>

在AndroidMANIFEST.xml中,在应用程序(针对整个应用程序)或活动(仅针对此活动)标记中设置主题属性

<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...
From the Activity (called 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);

}

答案 1 :(得分:0)

这是一个真正的解决方法,但我得到了我想要的东西。我在我的布局顶部放置了一个textview并给出了以下尺寸,使其看起来像标题栏

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vehicle_view"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

//Title bar
<TextView
    android:layout_width="fill_parent"
    android:layout_height="24dip"
    android:id="@+id/title_bar_text"
    android:background="#dddddd"
    android:textColor="#150517"
    android:text="Enter Text">
 </TextView> 
.............................    //Rest of layout elements
.............................
.............................
</LinearLayout>

如果某人有更好的答案以某种方式添加额外的标题栏而不改变我所做的布局,请发布它我会接受这个作为答案。