我有2个.xml,其中有一个imageView资源,它是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher30005"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher22" />
<Button android:id="@+id/bSetWall"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="400dp"
android:gravity="center"
android:text="Imposta come sfondo"
/>
</LinearLayout>
另一个。 xml我有按钮的资源以及它需要进入imageview的布局,是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="702dp"
android:layout_gravity="left"
android:orientation="vertical" >
<Button
android:id="@+id/bopentx"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:text="tattoo" />
<Button
android:id="@+id/bopensax"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:text="sand" />
<Button
android:id="@+id/bopenwx"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:text="wet" />
<Button
android:id="@+id/bopennx"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:text="naked" />
<Button
android:id="@+id/Bopenlx"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:text="loincloth" />
<Button
android:id="@+id/Bopenbx"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="left"
android:text="brazilian" />
</LinearLayout>
</ScrollView>
<ScrollView
android:id="@+id/Scroll1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<LinearLayout
android:id="@+id/Linear1"
android:layout_width="wrap_content"
android:layout_height="702dp"
android:layout_gravity="right"
android:orientation="vertical" >
//the image needs to go here when i click the button
</LinearLayout>
</ScrollView>
</LinearLayout>
在菜单类中我用过这个: 公共类菜单扩展Activity实现View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button) findViewById(R.id.bopennx);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bopennx:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_main, null);
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.Linear1);
ImageView newImageView=(ImageView)view.findViewById(R.id.imageView1);
newImageView.setImageResource(R.drawable.ic_brazilian1);
linearLayout.addView(newImageView);
break;
}
}});
}
答案 0 :(得分:0)
为了从.xml获取与您在contentView中设置的视图不同的视图(在您的情况下为R.layout.main),您必须对xml进行充气并在xml中搜索视图已经膨胀了。
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_xml, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);