android-如何将文本放在网格视图中的图像下方
在我的应用程序中,我想在网格视图中显示图像下方的文本。
以下是代码,但它不起作用。它显示了这样的异常
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): FATAL EXCEPTION: main
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at sra.gri.vie.Main$ImageAdapter.getView(Main.java:63)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.GridView.onMeasure(GridView.java:932)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.os.Looper.loop(Looper.java:123)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.reflect.Method.invoke(Method.java:521)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at dalvik.system.NativeStart.main(Native Method)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): Caused by: java.lang.ClassNotFoundException: android.view.linearlayout in loader dalvik.system.PathClassLoader[/data/app/sra.gri.vie-1.apk]
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): ... 26 more
05-31 17:23:32.017: INFO/ActivityManager(60): Process sra.gri.vie (pid 1262) has died.
grid_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#000080">
<imageview android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</imageview>
<textview android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#000000">
</textview>
</linearlayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Main.java
package sra.gri.vie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View MyView = convertView;
if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.grid_item, null);
// Add The Image!!!
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText(names[position] );
}
return MyView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
};
private String[] names={"Dog1","Dog2","Dog3","Dog4","Dog5","Dog6","Dog7","Dog8",};
}
}
谢谢
in my application i want to display the text below the image in grid view.
The following is the code but it is not working. it shows an exception like this
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): FATAL EXCEPTION: main
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at sra.gri.vie.Main$ImageAdapter.getView(Main.java:63)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.GridView.onMeasure(GridView.java:932)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.View.measure(View.java:8171)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.os.Looper.loop(Looper.java:123)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.reflect.Method.invoke(Method.java:521)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at dalvik.system.NativeStart.main(Native Method)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): Caused by: java.lang.ClassNotFoundException: android.view.linearlayout in loader dalvik.system.PathClassLoader[/data/app/sra.gri.vie-1.apk]
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-31 17:19:29.297: ERROR/AndroidRuntime(1262): ... 26 more
05-31 17:23:32.017: INFO/ActivityManager(60): Process sra.gri.vie (pid 1262) has died.
grid_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#000080">
<imageview android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</imageview>
<textview android:id="@+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColor="#000000">
</textview>
</linearlayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Main.java
package sra.gri.vie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Main.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View MyView = convertView;
if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.grid_item, null);
// Add The Image!!!
ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
tv.setText(names[position] );
}
return MyView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
};
private String[] names={"Dog1","Dog2","Dog3","Dog4","Dog5","Dog6","Dog7","Dog8",};
}
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将元素
linearlayout
重命名为LinearLayout
。我有同样的问题并以这种方式修复它。
Rename element
linearlayout
toLinearLayout
.I had the same issue and fixed it that way.
该错误抱怨线性布局(android.view.linearlayout)的包错误。正确的是 android.widget.LinearLayout。但我在您的代码中找不到任何对线性布局的引用。如果您可以粘贴完整的异常堆栈跟踪,可能会有所帮助。
The error complains about the wrong package for linearlayout (android.view.linearlayout). The correct one is android.widget.LinearLayout. But I couldn't find any reference to linear layout in your code. It might help if you can paste the full exception stack trace.