android custom window title sample sharing
- 2020-05-30 21:01:05
- OfStack
1. After completing the project, create an title.xml file in its layout folder as the file for the custom window title.
<?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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/hello_world"
android:textColor="#FF00FF"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text=" add " />
</LinearLayout>
2. Create the rectangle.xml file under res/drawable, and apply the gradient effect to the window.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- Fill color is gradient , You don't need the middle color startColor Start and end colors .-->
<gradient
android:angle="270"
android:endColor="#1DC9CD"
android:startColor="#A2E0FB"/>
<!-- Define internal spacing -->
<padding
android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
</shape>
3. Layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
4. Customize window Settings through activity background code.
package com.example.customertitle;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
// Custom title
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 1. Set to use a custom window
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
// 2. To introduce a custom title to a window xml Interface file
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
}
public void add(View v) {
Toast.makeText(this, " The button is clicked ", Toast.LENGTH_LONG).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
5, deploy the project, can display a custom window title. However, the custom window title is 1 point away from the left and right sides of the interface and is not completely covered. To solve this 1 problem, you need to override the window title of android. Below is the source code for the title of the android window.
<!--2. Pay attention to : System window interface file in Android System source code android-sdk-windows\platforms\android-8\data\res\layout Under the screen_custom_title.xml , as follows:
1.1 Linear layout -->
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:fitsSystemWindows="true">
<FrameLayout android:id="@android:id/title_container"
android:layout_width="match_parent"
android:layout_height="?android:attr/windowTitleSize"
style="?android:attr/windowTitleBackgroundStyle">
</FrameLayout>
<FrameLayout android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" />
</LinearLayout>
android:attr/windowTitleSize
android:attr/windowTitleBackgroundStyle
android:attr/windowContentOverlay
The values of the above properties are defined in themes.xml file under android-sdk-windows \platforms\ android-8 \data\res\values:
<style name="Theme">
<itemname="windowContentOverlay">@android:drawable/title_bar_shadow</item>
<itemname="windowTitleSize">25dip</item>
<itemname="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
</style>
@android:style/WindowTitleBackground Style in android-sdk-windows\platforms\android-8\data\res\values Under the styles.xml File definition:
<style name="WindowTitleBackground">
<itemname="android:background">@android:drawable/title_bar</item>
</style>
From the above, you can know the theme style of android. Now you need to inherit and override its style. The code is as follows
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- define 1 Style, overwrite the original theme style -->
<style name="myTheme" parent="android:Theme">
<item name="android:windowContentOverlay">@drawable/color</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/textViewBg</item>
</style>
<style name="textViewBg">
<item name="android:background">@drawable/rectangle</item>
</style>
</resources>
The definition of a color value
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CustomerTitle</string>
<string name="action_settings">Settings</string>
<string name="hello_world"> Custom title </string>
<drawable name="color">#00000000</drawable>
</resources>