简体   繁体   English

如何在Android中的活动titile旁边放置按钮?

[英]how to put button beside activity titile in android?

Hi i want to put button beside activity title in android. 嗨我想把按钮放在android的活动标题旁边。 I can change the text using setTitle() but I haven't find anything which allows to put button beside title of Activity. 我可以使用setTitle()更改文本,但我没有找到任何允许将按钮放在Activity标题旁边的内容。 I am using android 4.0.3. 我使用的是android 4.0.3。 Can anybody help me? 有谁能够帮我?

在此输入图像描述

Create xml file in your project name topbar.xml 在项目名称topbar.xml中创建xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="vertical" 
    android:background="@android:color/white">

    <TextView 
        android:id="@+id/title_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MyTitle: NameOfUser"
        android:layout_centerVertical="true"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_marginLeft="20dp"/>


    <Button 
        android:id="@+id/back_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="Btn"
        android:textColor="@android:color/white"
        android:layout_marginLeft="5dp"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

And add this layout in any layout by just writing: 只需编写以下内容即可在任何布局中添加此布局:

<include layout="@layout/topbar"/>

Hope this will help you. 希望这会帮助你。

You can use custom title bar or you can use no title theme for your Activity and set a linear layout as child layout above of your Activity's layout,so it seems that your activity has title bar . 您可以使用custom title bar ,也可以no title theme为活动使用no title theme ,并将线性布局设置为活动布局上方的子布局,因此您的活动似乎有标题栏

  1. Creating custom title bar: 创建自定义标题栏:
    edumobile.org edumobile.org
    londatiga.net londatiga.net
  2. Hidding the title bar of your application. 隐藏申请的标题栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM