Android自定義TitleBar 自定義標題欄 並進行事件處理
【原文:http://blog.sina.com.cn/s/blog_62d3ddc00100z5u6.html】
Android自定義TitleBar 自定義標題欄 並進行事件處理
安卓自帶的標題欄感覺很是難看,那麼我們可以自定義titlebar
首先建立自定義標題欄xml檔案 放在layout目錄下
<?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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="blog.sina.com.cn/ruipheng"
android:textColor="@android:color/background_dark"
android:textSize="20dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="註冊" />
</LinearLayout>
在程式碼中引入
//自定義標題欄
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.test);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
我們發現這樣的話,標題欄的高度是原生的,不能滿足我們的需求,於是我們在value目錄下建立styles.xml檔案
程式碼如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_window_title_background">
<item name="android:background">@drawable/skinpic_blue</item>
</style>
<style name="custom_title">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/custom_window_title_background</item>
</style>
</resources>
這樣自定義titlebar的工作就完成了,那麼怎樣處理titleBar中的事件呢!其實很簡單,和處理本頁面其他的控制元件一樣的.我曾經以為處理上面的空間與自定義Dialog中xml的檔案的方法一樣,實際上我是錯了,根本不需要那樣處理
Button btn = (Button) findViewById(R.id.button1);
final TextView text = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setText("我的新浪部落格");
//System.out.println("我的新浪部落格");
}
});
下面是效果,右邊為點選註冊後
轉載請註明出處:http://blog.sina.com.cn/ruipheng
相關文章
- 自定義事件相容處理物件事件物件
- Android自定義OnTouch事件Android事件
- Android 自定義View:處理事件分發(四)AndroidView事件
- Qt隱藏系統標題欄,使用自定義標題欄QT
- HarmonyOS NEXT 5.0自定義增強版導航欄元件|鴻蒙ArkUI自定義標題欄元件鴻蒙UI
- 自定義View事件之進階篇(四)-自定義Behavior實戰View事件
- 自定義View事件篇進階篇(二)-自定義NestedScrolling實戰View事件
- android自定義View——座標系AndroidView
- js自定義Android端手勢事件JSAndroid事件
- JavaScript自定義事件JavaScript事件
- Netty中自定義事件處理程式和監聽器Netty事件
- Siri自定義Intent以及處理Intent
- android自定義view(自定義數字鍵盤)AndroidView
- 將帶標籤的點雲輸入並進行歐幾里得聚類處理(自定義點雲)聚類
- 繫結自定義事件事件
- 自定義事件提醒程式事件
- Javascript中自定義事件JavaScript事件
- WPF 自定義附加事件事件
- WPF自定義路由事件路由事件
- 【Django】DRF自定義異常處理Django
- Android自定義圓形進度條Android
- 多型關聯自定義的型別欄位的處理多型型別
- uniapp自定義導航欄APP
- Android 自定義View:深入理解自定義屬性(七)AndroidView
- 直播平臺軟體開發,實現自定義標題欄
- Android自定義View整合AndroidView
- Android自定義遮罩層Android遮罩
- 自定義Android鍵盤Android
- Android 自定義UI元件AndroidUI元件
- android 自定義鍵盤Android
- Flutter自定義折線圖並新增點選事件Flutter事件
- [系列] Gin框架 - 自定義錯誤處理框架
- spring 自定義標籤Spring
- 自定義標籤FlowTagLayout
- Prometheus自定義指標Prometheus指標
- jquery自定義事件的使用jQuery事件
- iOS系統導航欄自定義標題動畫跳變解析iOS動畫
- restframework 異常處理及自定義異常RESTFramework
- Spring Cloud Gateway-自定義異常處理SpringCloudGateway