简体   繁体   English

如何在Android活动的标题栏中添加关闭按钮?

[英]How can I add close button in title bar of the activity in android?

I have created custom title bar as suggested by this link:-= http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ 我已创建此链接建议的自定义标题栏:-= http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/

This is my window_title.xml:- 这是我的window_title.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="50dip"

        android:paddingLeft="5dip"
        android:paddingBottom="10dip"

        android:background="#323331">

    <TextView android:id="@+id/header_label" 

        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"

        android:text="Preview Song"
        android:textSize="10dip"

        android:gravity="center_vertical"

        android:layout_alignParentLeft="true" 
        android:layout_marginRight="5dip"
        android:layout_marginTop="5dip">

     </TextView>  

    <ImageButton
        android:id="@+id/header" 
        android:src="@drawable/button_close"

        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" 
        android:layout_marginRight="10dip"
        android:layout_marginTop="6dip"

        android:background="@null" />

</RelativeLayout>

This is my custom_title.xml:- 这是我的custom_title.xml:

<resources>

    <style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="@android:style/Theme.Dialog">
        <item name="android:windowTitleSize">50dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

</resources>

But this is my result now- ![enter image description here][1] 但这是我现在的结果-![在此处输入图片描述] [1]

It means title content (preview song and close image) are too small, I want to increase height of this title bar. 这意味着标题内容(预览歌曲和关闭的图像)太小,我想增加此标题栏的高度。 I have increased the height of title bar but this is not increasing the size of title bar but the contents overlaps the title bar and these button and preview song does not display. 我增加了标题栏的高度,但这并没有增加标题栏的大小,但是内容与标题栏重叠,并且这些按钮和预览歌曲不显示。

Please suggest me what mistake I am doing ? 请告诉我我在做什么错?

You have to create custom title bar and hide android title bar in your activity you add just... 您必须创建自定义标题栏,并在您添加的活动中隐藏android标题栏。

android:theme="@android:style/Theme.Black.NoTitleBar" android:theme =“ @ android:style / Theme.Black.NoTitleBar”

and see this link:: 并查看此链接::

http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ http://www.helloandroid.com/tutorials/how-create-custom-titlebar http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ http://www.helloandroid.com/tutorials/how-create-custom-titlebar

well samir second link working fine for me. samir第二个链接对我来说很好。 Have a look at the following windows_title.xml file 看看下面的windows_title.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
    android:background="#323331">

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/header1" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical"
        android:paddingRight="5dip">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="5dip"
            android:textSize="11dip" />

        <Button
            android:id="@+id/icon"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/close"
            android:layout_marginRight="10dp" />

        <ProgressBar
            android:id="@+id/leadProgressBar1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_below="@+id/titleTvLeft"
            android:layout_marginBottom="3dp" />

    </LinearLayout>

</LinearLayout>
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
}

@Override
public boolean onSupportNavigateUp() {
    finish();
    return true;
}

您可以为自己的新自定义布局充气。

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

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