从Android上的默认设置制作自定义标题视图

Ste*_*lis 6 android

如何使用androids默认窗口标题样式来使我自己的类似TextView

我做了很多猜测,并制作了一个TextView,它具有默认标题栏所具有的一切,除了文本阴影(以及一些填充/边距等等).

这基本上是我尝试过的:

MainUI.xml

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}
Run Code Online (Sandbox Code Playgroud)

title_bar.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:textAppearance="@android:style/TextAppearance.WindowTitle"
  android:background="@android:drawable/title_bar"
  android:text="This is my new title" />
Run Code Online (Sandbox Code Playgroud)

编辑:

我在makemachineanddev找到了一些有趣的相关文章.

虽然我不喜欢它,但我从实现style.xml中复制了一些属性.

有没有办法避免以这种静态方式复制属性?

以下渲染几乎完美,不同之处在于事实上原版确实"剪切"了标题阴影的前2-3个像素,而我TextView没有.

<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:gravity="center_vertical"
  android:shadowColor="#BB000000"
  android:shadowRadius="2.75"
  android:singleLine="true"
  android:textAppearance="@android:style/TextAppearance.WindowTitle"
  android:background="@android:drawable/title_bar"
  android:text="This is my new title" />
Run Code Online (Sandbox Code Playgroud)

使用透明颜色覆盖默认的android:windowTitleBackgroundStyle也很重要,因为默认情况下包含一些不希望包装自定义标题栏的填充等.

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="CustomTheme" parent="android:Theme">
    <item name="android:windowTitleBackgroundStyle">@android:color/transparent</item>
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

请记住在您的主题中启用主题 AndroidManifest.xml

小智 0

试试这个它对我有用:

将其余代码放在适当的位置..

 super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
   setContentView(R.layout.main);
   getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main_header);
     TextView  home = (TextView) findViewById(R.id.home);
       if ( home != null ) {
            /*  your code here */
           home.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // the action that u want to perform
                Menu.this.finish();
                Intent i= new Intent(Main.this,Destination.class);
                startActivity(i);
            }
        });
        }
Run Code Online (Sandbox Code Playgroud)

创建 main_header 布局文件

<?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="match_parent"
    android:orientation="vertical" >


    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Main Menu"
            android:textStyle="bold"
            android:textSize="10pt" />

        <TextView
            android:id="@+id/home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView1"
            android:layout_alignBottom="@+id/textView1"
            android:layout_alignParentRight="true"
            android:textStyle="bold"
            android:text="Home"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </RelativeLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在这里,当我们执行代码时,它将显示一个自定义标题栏,其中有两个文本,当我们单击它时,它将进入下一个类(到您的目标类)。我们还可以更改颜色、字体大小等。还可以向自定义标题栏添加按钮等。