简体   繁体   English

如何创建Android自定义标题栏

[英]How to create Android custom titlebar

I try to build an application on Android. 我尝试在Android上构建应用程序。 And I'm new in Android. 我是Android新手。 But I don't know how to build a Title Bar like this. 但是我不知道如何建立这样的标题栏。 So we can give the application name like Seesmic and Komutta with the tab button. 因此,我们可以使用选项卡按钮将应用程序名称命名为Seesmic和Komutta。 Can anyone help me to give me the answer or just a link for that tutorial? 谁能帮助我给我答案,或者只是该教程的链接?

Thank you. 谢谢。

https://lh6.ggpht.com/Hf6XKfa9K0B-CvlV6tD6qj2Yt8wJcyJ7wa8vE9BVkBbUDm0Y2pqOxgxVf7auQgXrh0gR https://lh6.ggpht.com/Hf6XKfa9K0B-CvlV6tD6qj2Yt8wJcyJ7wa8vE9BVkBbUDm0Y2pqOxgxVf7auQgXrh0gR

https://lh4.ggpht.com/rwceS5ZK1IZkHHCVixbaXlsHXwstpmIO888aMC4U0uD2oa54NiGvphcp_penGK9Q9WE https://lh4.ggpht.com/rwceS5ZK1IZkHHCVixbaXlsHXwstpmIO888aMC4U0uD2oa54NiGvphcp_penGK9Q9WE

I'm sorry I can't upload the image, so I just can give the link for that image. 抱歉,我无法上传图片,因此只能提供该图片的链接。

这就是所谓的“动作栏”,您可以从Android 3.0开始自然地获取它,也可以在此处获取代码以在早期版本的android上进行操作

  1. Create a new project and name your main activity "MyActivity" 创建一个新项目并将您的主要活动命名为“ MyActivity”
  2. Go to res - drawable and create a new xml file and call it "custom_title_background" and put the following code: 转到res-drawable并创建一个新的xml文件,并将其命名为“ custom_title_background”,然后输入以下代码:

     <item android:top="20dp"> <shape android:shape="rectangle"> <gradient android:angle="90" android:endcolor="#9eacbf" android:startcolor="#8296af"> </gradient></shape> </item> 

This drawable will be used to set the background from custom_title_bar (from step 3) and to set the windowTitleBackgroundStyle from custom_title_style (from step 4) 此drawable将用于从custom_title_bar设置背景(从步骤3开始),并从custom_title_style设置windowTitleBackgroundStyle(从步骤4开始)

  1. Go to res-layout and create a new xml and name it "custom_title_bar". 转到res-layout并创建一个新的xml,并将其命名为“ custom_title_bar”。 Here you will create a layout with a text view like in the following code: 在这里,您将创建一个带有文本视图的布局,如以下代码所示:

     <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textColor="@android:color/white" android:textStyle="bold" android:id="@+id/custom_title_text" android:layout_centerInParent="true" android:shadowColor="@android:color/black" android:shadowRadius="3"/> 

  2. Go to res - values and create a new xml file and call it custom_title_style. 转到res-值并创建一个新的xml文件,并将其命名为custom_title_style。 Here you will create a new theme by overriding the existing one. 在这里,您将通过覆盖现有主题来创建一个新主题。 The name of the style "custom_title_theme" from below will be used into the manifest file to "activate" the new theme. 下面的样式“ custom_title_theme”的名称将用于清单文件中,以“激活”新主题。

    40dp @drawable/custom_title_background 40dp @ drawable / custom_title_background

  3. Now go to the AndroidManifest.xml file and put the new theme in the application tag. 现在转到AndroidManifest.xml文件,然后将新主题放入application标记中。

? 1 1个

  1. And at this last step, you have to go to the MyActivity class and put the following code: 在最后一步,您必须转到MyActivity类,并输入以下代码:

    import android.app.Activity; 导入android.app.Activity; import android.os.Bundle; 导入android.os.Bundle; import android.view.Window; 导入android.view.Window; import android.widget.TextView; 导入android.widget.TextView;

    public class MyActivity extends Activity { 公共类MyActivity扩展了Activity {

     @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //this must be called BEFORE setContentView requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //this must bew called AFTER setContentView getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar); //set the title TextView textView = (TextView)findViewById(R.id.custom_title_text); textView.setText("Custom Title"); } 

    } }

android网站上有一个演示,您可以检查CustomTitle以及如何在Android中创建自定义窗口标题

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

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