在另一个Layout.xml和Activity上设置后台资源

时间:2013-12-10 10:30:59

标签: android xml android-layout setbackground

如何设置onClick on imageview以在另一个layout.xml上设置backgroundresource即使我关闭应用程序然后再次打开它,另一个layout.xml上的backgroundresource是我点击的imageview吗?

这是我的代码,内容我想成为后台的图像

public class Main2Activity extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;

public Main2Activity(Context context) {
    inflater = LayoutInflater.from(context);

    items.add(new Item("Cindvia",      R.drawable.cindvia));
    items.add(new Item("Harugon",      R.drawable.harugon));
    items.add(new Item("Melodist",     R.drawable.melodist));
    items.add(new Item("Nabilaholic",  R.drawable.nabilaholic));
    items.add(new Item("Nat",          R.drawable.nat));
    items.add(new Item("Rona",         R.drawable.rona));
    items.add(new Item("Shanju",       R.drawable.shanju));
    items.add(new Item("Shinta",       R.drawable.shinta_n));
    items.add(new Item("Ve",       R.drawable.ve));
    items.add(new Item("Vienny",       R.drawable.vienny));
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int i) {
    return items.get(i);
}

@Override
public long getItemId(int i) {
    return items.get(i).drawableId;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = view;
    ImageView picture;
    TextView name;

    if(v == null) {
        v = inflater.inflate(R.layout.activity_main, viewGroup, false);
        v.setTag(R.id.picture, v.findViewById(R.id.picture));
        v.setTag(R.id.text, v.findViewById(R.id.text));
    }

    picture = (ImageView)v.getTag(R.id.picture);
    name = (TextView)v.getTag(R.id.text);

    Item item = (Item)getItem(i);

    picture.setImageResource(item.drawableId);
    name.setText(item.name);

    return v;
}

private class Item {
    final String name;
    final int drawableId;

    Item(String name, int drawableId) {
        this.name = name;
        this.drawableId = drawableId;
    }
}

}

这是我想要更改背景的.XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity"
android:background="@drawable/nat" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:shadowColor="#ffb4b4"
    android:shadowDx="2"
    android:shadowDy="2"
    android:shadowRadius="3"
    android:text="@string/eee_dd_mm_yyyy"
    android:textColor="#ffffff"
    android:textSize="17sp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:shadowColor="#ffb4b4"
    android:shadowDx="2"
    android:shadowDy="2"
    android:shadowRadius="4"
    android:text="@string/hh_mm_ss"
    android:textColor="#ffffff"
    android:textSize="37sp" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="40sp"
    android:layout_height="40sp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="17dp"
    android:layout_marginRight="17dp"
    android:src="@drawable/info"
    android:contentDescription="@string/todo"/>

它是一个下载我所有工作区的链接,我的意思是我正在使用的代码,

如果您想下载点击文件并下载

https://drive.google.com/file/d/0B7NPhjxHR4zbT2NaUDBRNnU4cWc/edit?usp=sharing

1 个答案:

答案 0 :(得分:1)

也许您应该创建一个SharedPreference来将最后ImageView点击设置为新背景。当您的应用程序打开时,您应该像这样使用首选项:

SharedPreferences myPrefs = this.
                         getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
drawableid = myPrefs.getInt("backgroundResourceId", defaultvalue); 
yourlayout.setBackground(drawableid);  

请参阅Google文档中的这个简单示例: Storage Options
请参阅此答案: Android: make background image changable
另请阅读: Changing background colour with SharedPreference in Android

更多信息:
How to use SharedPreferences in Android to store, fetch and edit values
SharedPreferences Documentation Android


示例
要创建SharedPreference,您可以按照本教程,简单易懂:
 Android User Session Management using Shared Preferences
这一个:Android SharedPreferences Example


这样做的方法
我不确定你要做什么,但它会是这样的:
Adapter中,设置OnClickListener

@Override
public View getView(int i, View view, ViewGroup viewGroup) {

    // It will be something like this, not sure:
    picture.setOnClickListener(new OnClickListener() {  
        public void onClick(View v) {  
            // This is a Toast to see the position of the selected item.  
            Toast.makeText(getApplicationContext(), "You clicked on the image position: " + i,Toast.LENGTH_LONG).show();  
            // so get the Id with this position  
            int idToBackground = getItemId(position);
            // put in SharedPreferences
            SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);  
            SharedPreferences.Editor editor = sp.edit();  
            editor.putInt("NewBackground", idToBackground);  
            editor.commit();
        } 

    });

    return v;
}  

另一种方式
Activity中,将适配器设置为GridView后,您可以制作OnItemClickListener

gridView.setAdapter(adapter);    
gridView.setOnItemClickListener(new OnItemClickListener() {  
    @Override   
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {  
        // do some stuff with the selected image  

        // get the id  
        int  idImage = v.getItemId(position); 

        // then put in SharedPref  
        SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);  
        SharedPreferences.Editor editor = sp.edit();  
        editor.putInt("NewBackground", idImage);  
        editor.commit();  
    }  
});  

编辑您的偏好:

SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("NewBackground", yourDrawableValue);
editor.commit();

最后,得到它:

SharedPreferences sp = getSharedPreferences("myApp", Activity.MODE_PRIVATE);
int myNewIntValue = sp.getInt("NewBackground", 0);

编辑:

要了解上下文,您有以下解决方案:
按活动,使用ActivityName.this
按应用,使用getApplicationContext()
按查看,使用YourView.getContext()

如果您需要通用的内容,请在活动中添加public static thecontextvariable并分配应用程序上下文。有了这个,您可以随时拨打YourActivity.thecontextvariable 希望这会有所帮助。