Android开发:定制Activity的标题栏(Titlebar)
2013-08-05 10:05
423 查看
编辑titlebar.xml,使其内容如下:
<?xmlversion="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="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/magnifier"
android:gravity="bottom"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="@string/app_name"
android:textColor="#FFFFFFFF"
android:textSize="14dip"
android:paddingTop="1dip"
/>
<EditText
android:id="@+id/searchparameter"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="ABCDEFGHIJ"
android:textSize="14dip"
android:layout_margin="1dip"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="OK"
android:textSize="14dip"
/>
</LinearLayout>
在上面的LinearLayout中,增加了以下控件:
一个ImageView,用于显示一个图标
一个TextView,用于显示应用的名称
一个EditText,用于接收输入
一个Button,用于测试
4. 修改CustomizeTitlebar.java,使之如下:
public class CustomizeTitlebar extends Activity
{
@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.titlebar);
}
}
以上加粗的两行很重要,而且必须要严格按照上面那样的顺序出现在代码中。即:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);必须出现在super.onCreate(savedInstanceState);之后,setContentView(R.layout.main);之前。其意思就是告诉系统,本程序要自己定义Titlebar;
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); 则必须出现在setContentView之后,其意思就是告诉系统,自定义的布局是R.layout.titlebar(即,我们前面编写的titlebar.xml)
到这里,不妨来运行一下,看看结果如何:
<?xmlversion="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="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/magnifier"
android:gravity="bottom"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="@string/app_name"
android:textColor="#FFFFFFFF"
android:textSize="14dip"
android:paddingTop="1dip"
/>
<EditText
android:id="@+id/searchparameter"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="ABCDEFGHIJ"
android:textSize="14dip"
android:layout_margin="1dip"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="38dip"
android:text="OK"
android:textSize="14dip"
/>
</LinearLayout>
在上面的LinearLayout中,增加了以下控件:
一个ImageView,用于显示一个图标
一个TextView,用于显示应用的名称
一个EditText,用于接收输入
一个Button,用于测试
4. 修改CustomizeTitlebar.java,使之如下:
public class CustomizeTitlebar extends Activity
{
@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.titlebar);
}
}
以上加粗的两行很重要,而且必须要严格按照上面那样的顺序出现在代码中。即:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);必须出现在super.onCreate(savedInstanceState);之后,setContentView(R.layout.main);之前。其意思就是告诉系统,本程序要自己定义Titlebar;
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar); 则必须出现在setContentView之后,其意思就是告诉系统,自定义的布局是R.layout.titlebar(即,我们前面编写的titlebar.xml)
到这里,不妨来运行一下,看看结果如何:
相关文章推荐
- Android开发:定制Activity的标题栏(Titlebar)
- AndroidGUI25:定制Activity的标题栏(Titlebar)
- AndroidGUI25:定制Activity的标题栏(Titlebar)
- Android:定制Activity的标题栏(Titlebar)
- Android:定制Activity的标题栏(Titlebar)
- AndroidGUI25:定制Activity的标题栏(Titlebar)(转载)
- AndroidGUI25:定制Activity的标题栏(Titlebar)
- Android开发 如何去除标题栏title titleBar
- 定制Activity的标题栏(Titlebar)
- 【Android Demo】自定义Activity的标题栏(Titlebar)
- Android开发笔记之主题背景的修改(设置Theme.NoTitleBar后Activity背景变成黑色)
- Android 自定义Activity的标题栏(Titlebar)
- Android开发之——自定义标题栏titlebar
- 【Android UI设计与开发】8.顶部标题栏(一)ActionBar 奥义·详解
- 自定义Activity标题栏(Title bar)和窗体显示状态操作(requestWindowFeature()的应用)
- android开发:activity继承AppCompatActivity 去掉标题栏
- 自定义Activity的标题栏(Titlebar)
- Android开发:去掉Activity的头部标题栏及全屏显示
- Android开发UI之去掉title bar
- 自定义Activity的标题栏(Titlebar)