开发者

How to set ImageViews in an external class so modifying them will change all Views that contain them?

So right now I have a main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.b" 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_color"
    android:textColorPrimaryInverse="#E97C03"
    >
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/background_color"
        android:textColorPrimaryInverse="#E97C03"
        > 
    <ImageView
        android:id="@+id/header_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        android:layout_gravity="left"
        android:background="@color/seperator_bg_color"
        />

        <ImageView
            android:id="@+id/build_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_camera"
            android:layout_gravity="left"

            />
        <ImageView
            android:id="@+id/saved_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_details"
            />
        <ImageView
            android:id="@+id/settings_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_menu_upload"
            />
    </LinearLayout>

    <TextView
        android:id="@+id/seperator_text"  
        android:layout_width="fill_parent" 
 开发者_JAVA百科       android:layout_height="wrap_content"
        android:padding="1dip"
        android:background="@color/seperator_bg_color"
        android:textColor="@color/seperator_text_color"
        android:textStyle="bold" 
        android:text="@string/header_build"
        android:textColorPrimaryInverse="#E97C03"
        />
<!--    <TextView  -->
<!--        android:id="@+id/selection" -->
<!--        android:layout_width="fill_parent" -->
<!--        android:layout_height="wrap_content" -->
<!--        />-->
    <ListView
        android:id="@android:id/list" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" 
        android:textColorPrimaryInverse="#E97C03"
        />
</LinearLayout>

Now, of course everything that uses the main.xml will have a header at the top(everything that is contained in the second, horizontal, LinearLayout.

In all of those views however, I have links with header_image, saved_image, build_image, etc that will take the user to a specific view. The problem is that if I make a change, I have to go and edit every single Activity that has those links.

Is there a way I can create an external class that will just contain information like this:

build =(ImageView)findViewById(R.id.build_image);
build.setOnClickListener(buildClick);

saved =(ImageView)findViewById(R.id.saved_image);
saved.setOnClickListener(savedClick);

prefs =(ImageView)findViewById(R.id.settings_image);
prefs.setOnClickListener(prefsClick);

instead of having that code in every single Activity?

EDIT: Essentially, I want to re-use my main.xml as my header for all activities, however where I set setOnClickListener I dont want to have to set those in every activity because if I change/add to the header, I have to dig through all the activities and change those.

So I want to take the code above and put it in a separate class. That way, in an activity, I call that class, and it will set those values for the header. Or set onClick settings in the XML, if that is possible.


I would make a baseActivity that contained application wide functionality. All activities that needs this functionality will just extend baseActivity instead.

Also you might want to extract that section from all layout files and put in an include layout.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜