【发布时间】:2014-10-23 22:59:34
【问题描述】:
我正在构建一个应用程序,其中有一个带有 imageview 和 textview 的列表视图,当我单击列表项时,我想将名称和图像传递给其他活动。
我已经设法做到了,但是当我在其他活动中传递图像时,图像看起来被拉伸了。如何解决这个问题?
我使用 Android Asset Studio 使我下载的图片可以以各种尺寸使用。
在列表视图中,图像看起来不错,但在其他活动中,图像看起来被拉伸了。
这是我的代码:
ListView lv = (ListView) findViewById(R.id.listView);
ArrayAdapter<ListaAdapterNext> adapter = new MySpecialAdapter();
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(NextActivity.this, ListItemActivity.class);
intent.putExtra("name", skonakia.get(position).getItemNextName().toString());
intent.putExtra("icon", skonakia.get(position).getImageNextId());
intent.putExtra("usage", skonakia.get(position).getItemUsage());
intent.putExtra("description", skonakia.get(position).getItemDescription());
startActivity(intent);
}
});
这里我将可绘制的 id 发送到其他活动
这是我收到它们的方式:
TextView tv = (TextView)findViewById(R.id.description);
TextView usage = (TextView)findViewById(R.id.usage);
String fetchedText = getIntent().getStringExtra("name");
String fetchedDescription = getIntent().getStringExtra("description");
String fetchedUsage = getIntent().getStringExtra("usage");
ImageView iv = (ImageView)findViewById(R.id.imageViewListItem);
Drawable image = getResources().getDrawable(getIntent().getIntExtra("icon", -1));
iv.setImageDrawable(image);
tv.setText(fetchedDescription);
usage.setText(fetchedUsage);
getActionBar().setTitle(fetchedText);
这是我用于列表视图和其他活动的 2 种布局,当有人单击列表项时
ListView 布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textViewListItem"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="-40dp"
android:id="@+id/imageView"
android:src="@drawable/ic_app" />
</LinearLayout>
ListItemClicked 布局:
<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="com.example.kostas.myapplication.ListItemActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/imageViewListItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/description"
android:id="@+id/textView" />
<TextView
android:id="@+id/description"
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<View
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:background="#000" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/usage"
android:id="@+id/textView2"
android:layout_marginTop="20dp"
android:layout_marginBottom="-15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/usage"
android:layout_marginTop="25dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buy"
android:id="@+id/buyButton"
android:layout_marginTop="50dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
【问题讨论】:
标签: android android-layout listview android-intent