开发者

Changing header button image and header text at runtime

开发者 https://www.devze.com 2023-03-12 01:49 出处:网络
I have a header common in all activities. header.xml <?xml version=\"1.0\" encoding=\"utf-8\"?> <LinearLayou开发者_JAVA技巧t xmlns:android=\"http://schemas.android.com/apk/res/android\"

I have a header common in all activities.

header.xml

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

<LinearLayou开发者_JAVA技巧t xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header_layout" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">

<!-- home button -->
<ImageButton android:id="@+id/header_home_button"
    android:src="@drawable/menu_info" android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

<!-- header text -->
<TextView android:id="@+id/header_title" android:layout_width="200dip"
    android:layout_height="wrap_content" android:gravity="center"
    android:text="Todays recipe :" android:textSize="20dp"
    android:textStyle="bold" />

</LinearLayout>  

However in different activities the title changes for textview and image for imageButton also changes. How to change them at runtime?

Thanks


you just need to get the both the Button and TextView by their id and set the image and text from your activity.

ImageButton _home  = (ImageButton)finndViewById(R.id.header_home_button);
_home.setImageResource(R.drawable.anyimage);


TextView _title  = (TextView )finndViewById(R.id.header_title);
_title.setText("Your title");


Use this in your Activity file:

TextView tv_edit = (TextView)findViewByID(R.id.header_title);
ImageButton btn_image = (ImageButton)findViewByID(R.id.header_home_button);

tv_edit.settext("Your text");
btn_imagesetImageResource(R.id.myImage);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号