简体   繁体   English

如何在Android的活动屏幕中添加其他标题栏?

[英]How to add an additional title bar to the activity screen in android?

How do i add a title bar to the screen in addition to the already existing one in android?I would also want to choose a color for this title bar. 如何我除了在Android上已经存在的添加标题栏到屏幕?我也想选择这个标题栏的颜色。

Can you tell me how this can be done? 你能告诉我怎么做吗?

This thread will get you started with building your own title bar in a xml file and using it in your activities 线程将使您开始在xml文件中构建自己的标题栏并在活动中使用它

Here is a brief summary of the content of the link above - This is just to set the color of the text and the background of the title bar - no resizing, no buttons, just the simpliest sample 这是上面链接内容的简要摘要-这只是为了设置文本的颜色和标题栏的背景-没有调整大小,没有按钮,只是最简单的示例

res/layout/mytitle.xml - This is the view that will represent the title bar 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 - We want to keep the default android theme and just need to change the background color of the title background. res / values / themes.xml-我们要保留默认的android主题,只需要更改标题背景的背景色即可。 So we create a theme that inherits the default theme and set the background style to our own style. 因此,我们创建一个继承默认主题的主题,并将背景样式设置为我们自己的样式。

<?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 - This is where we set the theme to use the color we want for the title background 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 - Set here the color you want res / values / colors.xml-在此处设置所需的颜色

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

In the AndroidMANIFEST.xml, set the theme attribute either in the application (for the whole application) or in the activity (only this activity) tags 在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);

}

This is a real workaround but i got what i wanted.I put a textview at the top of my layout and gave the following dimensions to make it look like a title bar 这是一个真正的解决方法,但是我得到了我想要的东西。我在布局的顶部放置了一个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>

If someone's got a better answer for adding an additional title bar in some way without altering the layout like i did please post it i'll accept that as the answer. 如果有人对以某种方式添加附加标题栏而不像我一样更改布局有更好的答案,请张贴它,我将其作为答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM