我正在开发贺卡应用程序。我在第一个 xml 文件中有两个 xml 文件,它包含按钮和列表视图。当我单击按钮列表将显示此列表占用活动空间。所以我们不能在 xml 文件中放置任何小部件。当单击列表项,我想在列表的同一位置显示 ImageView。为了实现这一点,我创建了另一个 xml 并将 ImageView 放在里面。现在,当我单击 ListItem 时,它会显示新的 xml,但不在同一个活动中。如何解决这个问题。我在这里附上了完整的源代码。这是我的 xml 文件 main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/backcolor"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/linearback" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/list_items" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="6dp"
android:background="@drawable/search" />
</LinearLayout>
<!--
<LinearLayout
android:id="@+id/mylayouttwo"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/imgview1"
android:layout_width="match_parent"
android:layout_height="300dp"></ImageView>
</LinearLayout>
-->
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
images.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@color/backcolor">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="234dp"
android:layout_marginTop="60dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
package com.triumph.greetings;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
public class Launch extends Activity implements OnClickListener {
boolean isListVisible=true;
ImageButton img1,img2;
ListView lv;
String[] festivales= new String[]{"Christmas","New year","BirthDay","Easter","Wedding","Valentine","Love","Nature"};
ImageView img;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1=(ImageButton)findViewById(R.id.imageButton1);
img2=(ImageButton)findViewById(R.id.imageButton2);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Launch.this,android.R.layout.simple_list_item_1,festivales);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int postion,
long arg3)
{
// TODO Auto-generated method stub
lv.setVisibility(View.INVISIBLE);
isListVisible=false;
Toast.makeText( Launch.this, ((TextView) v).getText(),Toast.LENGTH_LONG).show();
//state=true;
displayimages();
}
private void displayimages()
{
// TODO Auto-generated method stub
// fetch the images from server and shows in GridView
// call linear layout
setContentView(R.layout.images);
img=(ImageView)findViewById(R.id.imageView1);
img.setBackgroundColor(color.background_light);
}
});
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.imageButton1:
if(isListVisible)
{
lv.setVisibility(View.INVISIBLE);
isListVisible=false;
}
else
{
lv.setVisibility(View.VISIBLE);
isListVisible=true;
}
break;
//Toast.makeText(getBaseContext(), "Hi", Toast.LENGTH_LONG).show();break;
case R.id.imageButton2:
break;
}
}
}