2010-07-10 34 views
6

Làm cách nào để sử dụng kiểu tiêu đề cửa sổ mặc định của android để tự tạo kiểu tương tự TextView của riêng mình?Đặt chế độ xem tiêu đề tùy chỉnh từ mặc định trên Android

Tôi đã thực hiện rất nhiều dự đoán và tạo TextView có mọi thứ mà thanh tiêu đề mặc định có, ngoại trừ bóng văn bản (và một số phần đệm/lề, v.v.).

Dưới đây là về cơ bản những gì tôi đã cố gắng:

MainUI.xml

protected 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_bar); 
} 

title_bar.xml

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

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTitle" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:textAppearance="@android:style/TextAppearance.WindowTitle" 
    android:background="@android:drawable/title_bar" 
    android:text="This is my new title" /> 

Edit:

tôi thấy một số điều thú vị có liên quan tại makemachineanddev.

Mặc dù tôi không thích nó, tôi đã sao chép một số thuộc tính từ việc triển khai style.xml.

Có cách nào để tránh sao chép các thuộc tính theo cách tĩnh này không?

Sau đây được hiển thị gần như hoàn hảo, sự khác biệt thực tế là bản gốc đã "cắt" 2-3 pixel đầu tiên của bóng tiêu đề, trong khi TextView của tôi thì không.

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTitle" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:gravity="center_vertical" 
    android:shadowColor="#BB000000" 
    android:shadowRadius="2.75" 
    android:singleLine="true" 
    android:textAppearance="@android:style/TextAppearance.WindowTitle" 
    android:background="@android:drawable/title_bar" 
    android:text="This is my new title" /> 

Nó cũng quan trọng để ghi đè lên android mặc định: windowTitleBackgroundStyle với một màu trong suốt, bởi vì mặc định bao gồm một số đệm vv mà bạn không muốn được gói thanh tiêu đề tùy chỉnh của bạn.

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CustomTheme" parent="android:Theme"> 
    <item name="android:windowTitleBackgroundStyle">@android:color/transparent</item> 
    </style> 
</resources> 

Hãy nhớ để cho phép các chủ đề trong bạn AndroidManifest.xml

+0

Tôi không hiểu bạn đang cố gắng làm gì. Bạn có sẵn sàng sao chép thanh tiêu đề mặc định của Android không? – Macarse

+0

Có, miễn là tôi có thể sửa đổi nó. Tôi muốn thanh tiêu đề cũng hoạt động như một thanh tiến trình :-) – Steinbitglis

+1

không biết nếu điều này sẽ giúp hay không, nhưng [RomManager] (https://market.android.com/details?id=com.koushikdutta.rommanager&hl= vi) thực hiện điều đó khi bạn tải xuống roms, thanh tiêu đề đầy màu xám từ trái sang phải khi đang tải. nếu bạn chạy nó trong khi chạy trình xem Hierarchy, bạn có thể thấy chúng có thanh tiến trình và vòng tròn tiến trình. –

Trả lời

0

thử này nó làm việc cho tôi:

đưa phần còn lại của mã tại chỗ ..

super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.main); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.main_header); 
    TextView home = (TextView) findViewById(R.id.home); 
     if (home != null) { 
      /* your code here */ 
      home.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // the action that u want to perform 
       Menu.this.finish(); 
       Intent i= new Intent(Main.this,Destination.class); 
       startActivity(i); 
      } 
     }); 
     } 

tạo tệp sơ đồ main_header

<?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="vertical" > 


    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="Main Menu" 
      android:textStyle="bold" 
      android:textSize="10pt" /> 

     <TextView 
      android:id="@+id/home" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textView1" 
      android:layout_alignBottom="@+id/textView1" 
      android:layout_alignParentRight="true" 
      android:textStyle="bold" 
      android:text="Home" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

    </RelativeLayout> 

</LinearLayout> 

ở đây khi chúng tôi thực thi mã, nó sẽ hiển thị thanh tiêu đề tùy chỉnh nơi có hai văn bản trong đó và khi chúng tôi nhấp vào nó sẽ chuyển sang lớp tiếp theo (tới lớp đích của bạn). chúng tôi cũng có thể thay đổi màu sắc, kích thước phông chữ vv .. và cũng có thể thêm các nút, vv vào thanh tiêu đề tùy chỉnh.

Các vấn đề liên quan