android 自定義標題欄和自定義下拉選項PopupWindow

今天將醞釀已久的一個Android課程設計寫上CSDN記錄下來,可能過程一些錯誤的地方都忘記了,真不應該那麼遲才寫上來。因爲近端時間都在學習數學所有把精力都沒有集中在Android。

我先把我做得效果給甩上圖來。

這裏寫圖片描述

是一個可以發佈文字和圖文、視頻的一個小功能的App,中間是ListView,然後適配器adapter。


第一個要說的是 自定義標題欄

這裏寫圖片描述

在MainActivity.java中寫到

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//自定義標題欄
    setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mycustomtitle);//使用佈局文件來定義標題欄

新建了個xml文件:mycustomtitle.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:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:gravity="center"
    android:background="#FFF">



    <TextView android:id="@+id/header_text"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@+id/header_right_btn"
        android:text="Mind"
        android:layout_marginLeft="5dp"
        android:textSize="20sp"
        android:gravity="center"
        android:paddingLeft="40dp"
        android:textStyle="bold"
        android:textColor="#21cbc1"
        android:singleLine="true" />

    <RelativeLayout
        android:id="@+id/rl_topbar"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:gravity="center_vertical" >

    </RelativeLayout>

    <ImageButton
        android:id="@+id/addbtn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/add" />



</LinearLayout>

RelativeLayout在裏面只是起到定位的功能,在以上沒有什麼作用。

這是style文件的內容

    <style name="WindowTitleBackground" >
        <item name="android:background">#FFF</item>
    </style>
    <style name="MyTheme" parent="android:Theme">
        <item name="android:windowTitleSize">60dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>

這是AndroidManifest.xml的要修改和填寫的內容

    <activity
            android:name=".MainActivity"
            android:label="Mind"
            android:screenOrientation="portrait"
            android:theme="@style/MyTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

android:theme=”@style/MyTheme” 主要是這句 應用主題。

接下來是第二個內容:
這裏寫圖片描述

這是點擊按鈕,顯示的View. 這個是在網上學習的,至於原文章我也忘記了。後來我有修改過啦,他原本只是實現了功能,美化方面就沒有注意了。

代碼在過兩天再寫吧。眼睛都不舒服了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章