【问题标题】:set background resource on another Layout.xml and Activity在另一个 Layout.xml 和 Activity 上设置背景资源
【发布时间】:2013-12-10 10:30:58
【问题描述】:

如何在 imageview 上设置 onClick 以在另一个 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

【问题讨论】:

  • 不清楚你想做什么?
  • 我的意思是当我单击该网格视图上的图像时。我的 .xml 上的背景更改为我单击的图像。

标签: android xml android-layout setbackground


【解决方案1】:

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

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
希望这会有所帮助。

【讨论】:

  • 我真的是新手,我在哪里创建了 sharedPreference ?
  • 在适配器的 getview() 方法中创建它,当您更改图像时,只需将其保存在首选项中。当应用程序启动时,只需检查该首选项,如果它不为空,则获取图像并再次设置。
  • 请参阅粗体中的前两个问题。我认为这正是你必须做的。 @keshav 说得对,这些是以下步骤!
  • 但是如何在我的 imageview 上设置点击监听器?
  • 我更新了答案,并添加了指向 Google 存储选项文档的链接。读起来很有趣。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多