写自定义一个布局XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical" >
<TextView
android:id="@+id/titlebar"
android:textColor="#000"
android:paddingRight="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>然后在需要自定义标题的Activity里写入如下代码
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.menu_title);
TextView titleBar = (TextView)findViewById(R.id.titlebar);
titleBar.setText("欢迎你");requestWindowFeature一般在setContentView之前,setFeatureInt在setContentView之后。
R.layout.menu_title为上面XML布局文件的文件名
本文介绍了如何在Android中自定义布局XML文件,并将其应用于特定的Activity,通过设置窗口特性和布局实例化来实现自定义标题栏的功能。具体步骤包括创建布局文件、设置窗口特性、并使用XML布局作为标题栏,最后演示了如何在Activity中调用这些功能,展示了一个简单的应用示例。
1073

被折叠的 条评论
为什么被折叠?



