Android:简化许多类和xml
编辑:我无法让这个工作正常。可能是因为我不知道我在做什么。无论如何,这是我的代码。如果有人可以帮助,我会非常感激:我需要得到它的相应的MOD显示电池的心情图片...Android:简化许多类和xml
主题:
package com.cydeon.plasmamodz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Themes extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
ActionBar actionBar = getActionBar();
actionBar.hide();
Button Plus = (Button) findViewById(R.id.button1);
Button Blue = (Button) findViewById(R.id.button2);
Plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(Themes.this, Bmod.class);
i.putExtra("drawableResource", R.drawable.blue);
Themes.this.startActivity(i);
}
});
Blue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent a = new Intent(Themes.this, Bmod.class);
a.putExtra("drawableResource1", R.drawable.plus);
Themes.this.startActivity(a);
}
});
}
}
Bmods:
package com.cydeon.plasmamodz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class Bmod extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.battery);
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.blue);
ImageView img = (ImageView) findViewById(R.id.iv1);
img.setImageResource(R.drawable.blue);
Intent a = getIntent();
int drawableResource1 = a.getIntExtra("drawableResource1", R.drawable.plus);
ImageView img1 = (ImageView) findViewById(R.id.iv1);
img1.setImageResource(R.drawable.plus);
}
}
电池(XML):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bInstall"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Install" />
<Button
android:id="@+id/bReturn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Return" />
<ImageView
android:id="@+id/iv1"
android:layout_width="match_parent"
android:layout_height="800dp" />
</RelativeLayout>
你不应该需要50班和50点XML布局,如果他们中的每一个母鹿同样的事情。做一个活动和一个布局。当用户选择一些东西时,将某种类型的ID作为额外的Intent传递给第二个活动,以便它可以加载任何适合的项目。我不知道你的数据是如何建模的,但应该有一种方法来唯一标识每个选项(如果没有,你应该实现一个)。
您的第一个活动也不需要每个项目的按钮。使用一个ListView和一个适配器,然后你只需要为一行提供一个布局。
只能进行一项活动。里面的它让你的形象参考:
ImageView iv = (ImageView)findViewById(R.id.yourImgId);
然后设置图片你想喜欢的任何一个:
iv.setImageResource(R.drawable.battery_img_1);
谢谢。我会尝试一下,但我仍然对如何做到这一点感到困惑。如果可以的话,你能解释一下吗?也许举个例子。虽然你不需要。我真正想知道的是如何使用图像的网址来完成此操作。 – user2082169 2013-02-19 01:02:01
Intent i = new Intent(this, BaseClassForMod);
i.putExtra("drawableResource", R.drawable.this_mod_drawable);
startActivity(i);
然后在活动的onCreate():
Intent i = getIntent();
int drawableResource = i.getIntExtra("drawableResource", R.drawable.default);
//Get a reference to your ImageView...
imageView.setImageResource(drawableResource);
不要相信这个代码来编译,但这是一般的想法。使用相同的活动为他们所有人,传递意图适当的资源ID(例如为mod1,发送mod1的drawable ID),然后在活动中,检查该资源ID并设置它编程。
我明白如何做到这一点,但我有点困惑。我将如何在意图中传递适当的资源ID?就像我将如何发送我的第一个mod(mod1)的可绘制ID。 – user2082169 2013-02-24 19:44:06
您通过'R.drawable.mod1'而不是我拥有'R.drawable.this_mod_drawable'的地方。这只是一个整数值。 – kcoppock 2013-02-24 19:50:06
谢谢。还有一个问题。我将如何从url而不是drawable加载这个图像? – user2082169 2013-02-24 19:54:28
你的问题到底是什么? – 2013-02-18 21:41:48