Make custom title view from defaults on Android
How do I use androids default window title style to make my own similar TextView
?
I have done a lot of guessing and made a TextView that has everything that the default title bar has, except for the text shadow (and some padding/margins etc. I think).
Here's essentially what I've tried:
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);
}
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" />
Edit:
I found some interesting related articles at makemachine and anddev.
Although I don't like it, I copied some attributes from the implementation style.xml.
Is there any way to avoid copying the attributes in this static way?
The following rendered almost perfect, the difference is in fact that the original did "cut" the first 2-3 pixels of the title shadow, while my TextView
doesn't.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
androi开发者_运维知识库d: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" />
It's also important to override the default android:windowTitleBackgroundStyle with a transparent color, because the default includes some padding etc. that you don't want to be wrapping your custom title bar.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@android:color/transparent</item>
</style>
</resources>
Remember to enable the theme in your AndroidManifest.xml
try this it worked for me:
put the rest of the code in place..
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);
}
});
}
create a main_header layout file
<?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>
here when we execute the code it will show a custom title bar where there are two text's in it and when we click on that it will go the next class ( to your destination class). we can also change the color , size font etc.. and also add buttons etc to the custom title bar.
精彩评论