Android:自訂標題列

來源:互聯網
上載者:User

因為預設的標題列是只顯示應用程式名稱的,如果想在標題列上加上一些按鈕或更改下樣式就要自己改下代碼下,具體步驟如下:

 

  1. 在activity類中的onCreate方法中添加幾行代碼
    public class MainActivity extends Activity {    @Override    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.title_bar);                //更新標題列中按鈕的字型大小        Button btnManage = (Button) findViewById(R.id.btn_manage);        btnManage.setTextSize(10);

    主要就是這兩行:

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    這行放在setContentView前面

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);

    這行放在setContentView後面,其中的R.layout.title_bar就是我們的自訂layout

  2. 建立自訂的layout,替換架構預設的標題列,
    <?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="wrap_content"    android:gravity="center_vertical"    android:orientation="horizontal" >    <TextView        android:id="@+id/text_app_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="2dip"        android:layout_marginLeft="10dip"        android:textColor="#fff"        android:text="@string/app_name" />    <Button        android:id="@+id/btn_manage"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:layout_marginTop="4dip"        android:layout_alignParentRight="true"        android:paddingBottom="5dip"        android:paddingTop="0dip"        android:textColor="#006400"        android:text="@string/btn_manage" /></RelativeLayout>

  3. 這樣就搞定了,最後看下效果

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.