android-自定义窗口标题

通常窗口标题是在AndroidManifest.xml中通过android:label="@string/app_name"或setText方法来设置,如果我们想在窗体标题中放置自定义标题如:图片或按钮等之类的组件,就无从下手了。

res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

res/layout/title.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="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff000000"
        android:text="这是定制的title" 
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加" 
        android:onClick="add"/>

</LinearLayout>

 MainActivity.java

package org.com.customtitle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
    }
    public void add(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "执行了add方法", Toast.LENGTH_LONG).show();
    }
}

 res/values/style.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="myTheme" parent="android:Theme">
        <item name="android:windowContentOverlay">@drawable/nonecolor</item>
         <item name="android:windowTitleSize">44dp</item>
         <item name="android:windowTitleBackgroundStyle">@style/mytitlebackground</item>
    </style>

     <drawable name="nonecolor">#00000000</drawable>   
      <style name="mytitlebackground">
          <item name="android:background">@drawable/rectangle</item>
      </style>  
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.com.customtitle"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" android:theme="@style/myTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android自定义dialog是一种可以根据开发者的需求进行自定义的对话框。通过自定义dialog,开发者可以实现特定的界面设计和功能。根据引用,我们可以使用AlertDialog.Builder来创建一个普通的弹出框。而根据引用,我们可以通过简单的设置和技巧来实现Android自定义dialog。如果想要对话框具有自定义布局,可以创建一个布局文件,然后通过调用AlertDialog.Builder的setView()方法将其添加到对话框。默认情况下,自定义布局会填充对话框窗口,但是仍然可以使用AlertDialog.Builder的其他方法来添加按钮和标题。此外,开发者还可以完全自定义一个Dialog,需要进行以下几步: 1. 定义一个style 2. 创建一个继承自Dialog的子类 3. 在子类实现自定义布局和相关功能 4. 在应用调用自定义dialog的实例来显示对话框。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [android自定义Dialog](https://blog.csdn.net/weixin_41796401/article/details/96636408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Android自定义dialog简单实现方法](https://download.csdn.net/download/weixin_38736562/12800768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值