android 自定义titlebar后 按钮失效,Android标题栏中添加返回按钮功能

本文介绍了如何在Android开发中自定义标题栏并添加返回按钮功能。通过创建自定义类`CustomTitle`,设置布局文件,监听点击事件,以及在需要的Activity中调用,实现了点击返回按钮关闭Activity的效果。同时,文章提供了详细代码示例,包括XML资源文件、Style配置以及在AndroidManifest.xml中的应用。
摘要由CSDN通过智能技术生成

标题栏中的返回按钮在实际使用中用的比较多,今天就来讲讲我在项目开发中的使用经历,话不多说,还是直接上源码,上源码是最给力的。

一、 编写自定义类

public class CustomTitle {

private static Activity mActivity;

public static void getCustomTitle(Activity activity, String title) {

mActivity = activity;

mActivity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

mActivity.setContentView(R.layout.custom_title);

mActivity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,

R.layout.custom_title);

TextView textView = (TextView) activity.findViewById(R.id.head_center_text);

textView.setText(title);

Button titleBackBtn = (Button) activity.findViewById(R.id.TitleBackBtn);

titleBackBtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Log.d("Title back","key down");

mActivity.finish();

}

});

}

}

二 、 xml资源,在layout中定义custom_title

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/TitleBackBtn"

android:layout_width="50dp"

android:layout_height="wrap_content"

android:gravity="center"

android:layout_alignParentRight="true"

android:background="@android:drawable/ic_menu_revert"/>

android:id="@+id/head_center_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:text=""

android:textSize="25sp"

android:textColor="#FFFFFF"

/>

三 、 在需要调用的activity中调用

public class InformationActivity extends Activity{

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

CustomTitle.getCustomTitle(this, "个人信息");

setContentView(R.layout.informationactivity);

.......................

}

}

四 、 在res/values/style.xml中添加style定义

@style/TitleBarBackground

50dp

五 、 在AndroidManifest.xml中对InformationActivity添加支持

android:name="com.xxx.InformationActivity"

android:theme="@style/MyCustomTheme"

android:screenOrientation="landscape" />

OK,完成上述几个步骤,就可以了。

以上所述是小编给大家介绍的Android标题栏中添加返回按钮功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现自定义标题栏可以通过使用QML的Rectangle等组件来实现,并在其添加需要的控件,例如按钮、标签、图标等。同时,为了实现与原生窗体相似的功能,可以使用QML的Window组件,并将其属性设置为透明,然后在自定义标题栏添加控件时,将它们放置在Window组件的边缘位置,以达到与原生窗体相似的效果。此外,还可以通过使用QML的MouseArea组件来实现拖拽窗口的功能。具体实现方法可以参考以下示例代码: ``` import QtQuick 2.15 import QtQuick.Window 2.15 Window { visible: true width: 640 height: 480 title: "" Rectangle { id: titleBar width: parent.width height: 30 color: "#2d2d2d" MouseArea { anchors.fill: parent drag.target: parent } Label { text: "Custom Title Bar" font.bold: true color: "#ffffff" anchors.centerIn: parent } Button { text: "X" font.bold: true color: "#ffffff" anchors.right: parent.right anchors.verticalCenter: parent.verticalCenter onClicked: { Qt.quit(); } } } Rectangle { id: content anchors.top: titleBar.bottom anchors.left: parent.left anchors.right: parent.right anchors.bottom: parent.bottom color: "#ffffff" } } ``` 在这个示例,我们使用了一个Rectangle组件作为自定义标题栏,并在其添加了一个Label和一个Button。为了实现拖拽窗口的功能,我们在自定义标题栏添加了一个MouseArea组件,并将drag.target设置为父组件。另外,我们还使用了另一个Rectangle组件作为窗口的内容区域,并通过anchors属性将其与自定义标题栏对齐。最后,我们将Window的title属性设置为空字符串,以隐藏原生窗口的标题栏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值