2010-03-10 45 views
16

bạn bè,làm thế nào để đặt tiêu đề tùy chỉnh TextView Value tự động trong Android?

tôi đã tạo ra thanh tiêu đề tùy chỉnh sử dụng tập tin titlebar.xml sau bằng mã

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myTitle" 
    android:text="This is my new title" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textColor="@color/titletextcolor" 
    android:layout_marginLeft="25px" 
    android:paddingTop="3px" 
    /> 

và mã java để hiển thị thanh tiêu đề tùy chỉnh trên từng hoạt động.

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle); 

super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


} 

bây giờ tôi muốn đặt giá trị xem văn bản động trong mỗi hoạt động có thể hướng dẫn tôi cách tôi có thể đạt được điều này không?

sử dụng findviewbyid tại đây tôi không nhận được tham chiếu của textview đó để đặt giá trị vì bố cục chính không chứa bất kỳ hộp văn bản nào có tên như vậy nhưng mytitle.

bất kỳ trợ giúp nào sẽ được đánh giá cao.

+0

hướng dẫn tôi đi theo http: //stackoverflow.com/questions/2251714/set-title-background-color – UMAR

Trả lời

14

Đây là cách để thiết lập các tùy chỉnh tiêu đề:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

    setContentView(R.layout.main); 

    if (customTitleSupported) { 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle); 
    } 

    final TextView myTitleText = (TextView) findViewById(R.id.myTitle); 
    if (myTitleText != null) { 
     myTitleText.setText("========= NEW TITLE =========="); 
     myTitleText.setBackgroundColor(Color.GREEN); 
    } 


} 
+3

myTitleText là nu Sẽ bởi vì nó không phải là trong bố trí chính thân yêu. đó là bố cục mytitle. khi chúng ta sử dụng r.id.somthing, nó trông trong layout được viết trong setContentView (R.layout.main); trong trường hợp của tôi nó không phải là chính nó là ở một nơi khác. hy vọng bạn hiểu câu hỏi của tôi. – UMAR

+0

Kiểm tra trước, nói sau. Vậy, bạn nghĩ tựa đề xanh đến từ đâu? –

+0

Xin chào, Điều này đang thay đổi màu nền của thanh tiêu đề nhưng tôi thấy một số khoảng trống ở cả hai bên. Nó không đẹp bằng cách đó. Bạn có thể giúp tôi giải quyết vấn đề đó không? –

7

Bạn đã thử phương pháp setTitle() của Activity?

0

Hãy thử như sau:

@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.mytitle); 
    TextView mytitletext = (TextView) findViewById(R.id.myTitle); 
    mytitletext.setText("========= NEW TITLE =========="); 
} 

Bạn có thể sử dụng hierarchyviewer để xem xem bạn có thể truy cập (bạn sẽ thấy id của nó trong đồ thị phân cấp)

1

my_title.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/header" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:background="#d4e9a9"> 

    <ImageView android:src="@drawable/jetpack" 
     android:layout_width="wrap_content" android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" android:id="@+id/back" 
     android:layout_height="wrap_content" android:layout_alignParentTop="true" /> 

    <TextView android:id="@+id/title" android:layout_width="wrap_content" 
     android:gravity="center_vertical" android:textSize="20px" 
     android:textColor="#ffffff" android:layout_alignParentRight="true" 
     android:text="New Title" android:background="#a5c639" 
     android:layout_height="wrap_content" android:layout_alignParentTop="true" 
     android:padding="9dip" android:layout_margin="5dip" /> 
</RelativeLayout> 

Code:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  
setContentView(R.layout.main); 
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);  

((TextView)findViewById(R.id.title)).setText("gradient shadow"); 

findViewById(R.id.back).setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     ((TextView)findViewById(R.id.title)).setText("loce"); 
    } 
}); 

Bởi vì tiêu đề tùy chỉnh mặc định là cố định, bạn nên viết thư cho mình một chủ đề:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/MyTheme"> 
14

SDK 2.1+

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main); 
    setTitle("========= NEW TITLE =========="); 
} 
+0

Đá giải pháp này! Bằng cách nào đó các tính năng mới nhất tất cả được ẩn dưới đống các giải pháp ngẫu nhiên. Cảm ơn :-) –

+0

hiện nó đặt tiêu đề ở trung tâm? –

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