【Android UI】自定义带按钮的标题栏

简介: 自定义标题栏在很多的android app中很常见,可以说是一种很有用的UI设计方法。自 己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下: 一:大致流程 1.      对指定的android activity设置自定义主题风格,其中自定义主题风格是关键 在android 4.

自定义标题栏在很多的android app中很常见,可以说是一种很有用的UI设计方法。自

己也本着学习的态度,经过一番各种坑,终于实现了,现总结如下:

一:大致流程

1.      对指定的android activity设置自定义主题风格,其中自定义主题风格是关键

在android 4.0以上版本中如果使用Theme.Holo或者Theme.Light等,程序会

一直报错误-you cannot combine custom title with other feature titles

2.      在对应的Activity中加入代码

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mycustomtitle);

3.      在styles.xml使用如下的自定义主题。

<resources>

    <style name="WindowTitleBackground" >   
             
    </style>
    <style name="MyTheme" parent="android:Theme">
        <item name="android:windowTitleSize">60dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>                
    </style>

</resources>

 二:测试MainActivity源代码

MainActivity.java

package com.example.title_bar;

import android.app.Activity;
import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; public class MainActivity extends Activity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebarstyle); button=(Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,test_title_bar.class); startActivity(intent); } }); } }

test_title_bar.java

package com.example.title_bar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class test_title_bar extends Activity{

      @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
         // 去标题
          //requestWindowFeature(Window.FEATURE_NO_TITLE);
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
            setContentView(R.layout.titlebartest);  
            //设置标题为某个layout,标题样式
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebarstyle);

            //设置标题栏的标题
            settitle("hello","我是测试页面二","world");
        }

    private void settitle(String btn1str,String string,String btn2str) {
        // TODO Auto-generated method stub
        
        Button btnback=(Button) findViewById(R.id.back);
        Button btnnext=(Button) findViewById(R.id.next);
        TextView tvtitle=(TextView) findViewById(R.id.title);
        
        btnback.setText(btn1str);
        tvtitle.setText(string);
        btnnext.setText(btn2str);
    }


}

三:XML资源文件

titlebarstyle.xml的内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:layout_height="fill_parent" >
    <Button  android:id="@+id/header_left_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_centerVertical="true"
        android:text="back"
        android:textColor="#000000"/>
    
        <TextView android:id="@+id/header_text"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toRightOf="@+id/header_left_btn"
            android:layout_toLeftOf="@+id/header_right_btn"
            android:text="My Title Bar"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#FFFFFF"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:singleLine="true" />
           
        <Button  android:id="@+id/header_right_btn"
            android:layout_width="wrap_content"
             android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
             android:layout_centerVertical="true"
             android:text="next"  
             android:textColor="#000000"/>

</RelativeLayout>

activity_main.xml

<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="com.example.title_bar.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是测试页一" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="97dp"
        android:text="去测试页面二" />

</RelativeLayout>

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是测试界面二" />

</LinearLayout>

后别忘记在android的manifest配置文件中加上自定义的主题

android:theme="@style/MyTheme" 

界面一:

 

界面二:

 

遗失的拂晓
目录
相关文章
|
24天前
|
消息中间件 安全 数据处理
Android为什么不能在子线程更新UI
Android为什么不能在子线程更新UI
26 0
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
18天前
|
JavaScript
Vue给Element UI的el-popconfirm绑定按钮事件
Vue给Element UI的el-popconfirm绑定按钮事件
|
3天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
21 1
|
3天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
23 0
|
3天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
18 0
|
13天前
|
编解码 Android开发 UED
安卓UI/UX设计原则:打造引人入胜的用户体验
【4月更文挑战第13天】本文探讨了安卓UI/UX设计的关键原则,包括一致性、简洁性、反馈、清晰性、效率和适应性。一致性要求视觉和行为保持一致,利用系统UI;简洁性减少用户行动,简化导航;反馈需即时且明确;清晰性强调表达清晰,布局有序;效率关注性能优化和任务简化;适应性涉及多设备适配和用户多样性。遵循这些原则,可创建出色应用,提供无缝用户体验。设计应持续迭代,适应技术发展和用户需求。
|
16天前
|
XML 移动开发 Android开发
构建高效安卓应用:采用Jetpack Compose实现动态UI
【4月更文挑战第10天】 在现代移动开发中,用户界面的流畅性和响应性对于应用的成功至关重要。随着技术的不断进步,安卓开发者寻求更加高效和简洁的方式来构建动态且吸引人的UI。本文将深入探讨Jetpack Compose这一革新性技术,它通过声明式编程模型简化了UI构建过程,并提升了性能与跨平台开发的可行性。我们将从基本概念出发,逐步解析如何利用Jetpack Compose来创建具有数据动态绑定能力的安卓应用,同时确保应用的高性能和良好用户体验。
15 0
|
18天前
|
XML Java Android开发
Android之UI基础控件
Android之UI基础控件
|
19天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。