Android的画廊标题 [英] Android gallery with caption
问题描述
我一直在关注画廊的例子在这里:的http://开发商.android.com /资源/教程/视图/ HELLO-gallery.html
和添加文本,从这里下的图像显示:<一href="http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/" rel="nofollow">http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/
不过,我正从LogCat中此错误
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):致命异常:主要
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):java.lang.ClassCastException:android.widget.Gallery $的LayoutParams不能转换为android.widget.LinearLayout $的LayoutParams
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.View.measure(View.java:10828)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.ViewGroup.measureChild(ViewGroup.java:4322)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.AbsSpinner.onMeasure(AbsSpinner.java:206)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.View.measure(View.java:10828)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.View.measure(View.java:10828)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.View.measure(View.java:10828)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:1890)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.View.measure(View.java:10828)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.ViewRoot.performTraversals(ViewRoot.java:909)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.os.Handler.dispatchMessage(Handler.java:99)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.os.Looper.loop(Looper.java:132)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在android.app.ActivityThread.main(ActivityThread.java:4025)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在java.lang.reflect.Method.invokeNative(本机方法)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在java.lang.reflect.Method.invoke(Method.java:491)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-22 17:21:02.193:ERROR / AndroidRuntime(1444):在dalvik.system.NativeStart.main(本机方法)
我假设图库视图不会线性布局工作?我相信,即时得到上述错误的getView()。任何人都知道的一种方式来解决这个问题?
GalleryActivity.java
包android.gallery;
进口android.app.Activity;
进口android.content.Context;
进口android.content.res.TypedArray;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.BaseAdapter;
进口android.widget.Gallery;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.Toast;
公共类GalleryActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
画廊G =(图库论坛)findViewById(R.id.gallery);
g.setAdapter(新ImageAdapter(本));
g.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图父,视图V,INT位置,长的id){
Toast.makeText(GalleryActivity.this,+位置,Toast.LENGTH_SHORT).show();
}
});
}
公共类ImageAdapter扩展了BaseAdapter {
INT mGalleryItemBackground;
私人语境mContext;
私人整数[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
公共ImageAdapter(上下文C){
mContext = C;
TypedArray A = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground,0);
a.recycle();
}
公众诠释getCount将(){
返回mImageIds.length;
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
视图V;
LayoutInflater李= getLayoutInflater();
V = li.inflate(R.layout.caption,NULL);
TextView的电视=(TextView中)v.findViewById(R.id.caption_text);
tv.setText(测试);
ImageView的I =(ImageView的)v.findViewById(R.id.image_icon);
i.setImageResource(mImageIds [位置]);
i.setLayoutParams(新Gallery.LayoutParams(150,100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
返回伏;
}
}
}
caption.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent&GT;
&LT; ImageView的
机器人:ID =@ + ID / IMAGE_ICON
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT&GT;
&LT; / ImageView的&GT;
&LT;的TextView
机器人:文本=TextView的
机器人:ID =@ + ID / caption_text
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT&GT;
&LT; / TextView的&GT;
&LT; / LinearLayout中&GT;
main.xml中
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;画廊
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID /画廊
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT&GT;
&LT; /画廊&GT;
我已经试过这一个,发现解决这个的方法之一。请试试这个也:
main.xml中
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; LinearLayout中的android:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT机器人:方向=垂直安卓重力=中心的xmlns:机器人=http://schemas.android.com/apk/res/机器人&GT;
&LT;画廊机器人:ID =@ + ID / galleryid
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent/&GT;
&LT; / LinearLayout中&GT;
image_gallery_items.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout机器人:ID =@ + ID / LinearLayout01
机器人:layout_width =FILL_PARENT的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =WRAP_CONTENT机器人:填充=3SP机器人:方向=垂直安卓重力=center_horizontal&GT;
&LT; ImageView的机器人:ID =@ + ID /图片机器人:SRC =@可绘制/图标
机器人:layout_height =110dp机器人:layout_width =130dp机器人:layout_gravity =中心&GT;&LT; / ImageView的&GT;
&LT; TextView的Android版本:文本=TextView的机器人:ID =@ + ID / textView1机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT&GT;&LT; / TextView的&GT;
&LT; / LinearLayout中&GT;
GalleryExample.java
公共类GalleryExample延伸活动{
私人画廊galleryView;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
galleryView =(图库论坛)findViewById(R.id.galleryid);
galleryView.setAdapter(新ImageAdapter(本));
}
}
ImageAdapter.java
公共类ImageAdapter扩展了BaseAdapter {
私人活动的活动;
私有静态LayoutInflater吹气= NULL;
公共ImageAdapter(活性){
活性= A;
充气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
公众诠释getCount将(){
返回data.length;
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共静态类ViewHolder {
公共TextView的文字;
公众ImageView的形象;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看VI = convertView;
ViewHolder持有人;
如果(convertView == NULL){
VI = inflater.inflate(R.layout.image_gallery_items,NULL);
持有人=新ViewHolder();
holder.text =(TextView中)vi.findViewById(R.id.textView1);
holder.image =(ImageView的)vi.findViewById(R.id.image);
vi.setTag(保持器);
}
其他
支架=(ViewHolder)vi.getTag();
holder.text.setText(名字[位置]);
最终诠释stub_id =数据[位置]
holder.image.setImageResource(stub_id);
返回六;
}
私人INT []数据= {
R.drawable.imag1,R.drawable.imag2,
R.drawable.imag3,R.drawable.imag4,
R.drawable.imag5,R.drawable.imag6
};
私有String []名= {
Image1的,图像2,
的Image3,为image4,
Image5,Image6
};
}
和在AndroidManifest.xml中指定GalleryExample而已。
I have been following the gallery example here: http://developer.android.com/resources/tutorials/views/hello-gallery.html
and adding a text to show up under the image from here: http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/
However, I am getting this error from LogCat
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): FATAL EXCEPTION: main
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): java.lang.ClassCastException: android.widget.Gallery$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewGroup.measureChild(ViewGroup.java:4322)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:206)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.os.Looper.loop(Looper.java:132)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.app.ActivityThread.main(ActivityThread.java:4025)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at java.lang.reflect.Method.invoke(Method.java:491)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at dalvik.system.NativeStart.main(Native Method)
I am assuming the gallery view wont work with linear layout? I believe im getting the above error from getView(). Anyone know of a way to fix this?
GalleryActivity.java
package android.gallery;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.caption, null);
TextView tv = (TextView) v.findViewById(R.id.caption_text);
tv.setText("test");
ImageView i = (ImageView)v.findViewById(R.id.image_icon);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return v;
}
}
}
caption.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:text="TextView"
android:id="@+id/caption_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Gallery>
I have tried this one and found one way to solve this. Please try this also:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" xmlns:android="http://schemas.android.com/apk/res/android">
<Gallery android:id="@+id/galleryid"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
image_gallery_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:padding="3sp" android:orientation="vertical" android:gravity="center_horizontal">
<ImageView android:id="@+id/image" android:src="@drawable/icon"
android:layout_height="110dp" android:layout_width="130dp" android:layout_gravity="center"></ImageView>
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
GalleryExample.java
public class GalleryExample extends Activity {
private Gallery galleryView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
galleryView = (Gallery)findViewById(R.id.galleryid);
galleryView.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater=null;
public ImageAdapter(Activity a) {
activity = a;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public TextView text;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.image_gallery_items, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.textView1);
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(name[position]);
final int stub_id=data[position];
holder.image.setImageResource(stub_id);
return vi;
}
private int[] data = {
R.drawable.imag1, R.drawable.imag2,
R.drawable.imag3, R.drawable.imag4,
R.drawable.imag5, R.drawable.imag6
};
private String[] name = {
"Image1", "Image2",
"Image3", "Image4",
"Image5", "Image6"
};
}
And in the AndroidMAnifest.xml specify GalleryExample only.
这篇关于Android的画廊标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!