Android,自定义视图不会填充图库中的屏幕

问题描述:

我想显示一个自定义视图,该图像由一个垂直LinearLayout中的ImageView上方的TextView组成,一次一个一个地位于一个图库中。Android,自定义视图不会填充图库中的屏幕

的问题是,我的自定义视图不填充屏幕。我可以看到其他观点的一部分,我不想要这个问题。

这里是我的自定义视图的XML:gallery_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gallery_item_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
> 
    <TextView 
     android:id="@+id/gallery_item_cardname" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textSize="20dp" 
     android:textStyle="bold" 
     android:text="@string/contrebandiers_lvl1" 
     android:textColor="@android:color/darker_gray" 
    /> 
    <ImageView 
     android:id="@+id/gallery_item_cardimg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerInside" 
     android:contentDescription="@string/app_name" 
     android:src="@drawable/contrebandiers_lvl1" 
    /> 
</LinearLayout> 

这里是我的方法适配器的getVew的代码:GTRoundDeckAdapter

public View getView(int position, View convertView, ViewGroup parent) 
{ 
    GalleryItem galleryItem; 

    if(convertView == null) 
    { 
     galleryItem = new GalleryItem(); 
     convertView = mInflater.inflate(R.layout.gallery_item, null); 
     galleryItem.cardName = (TextView) convertView.findViewById(R.id.gallery_item_cardname); 
     galleryItem.cardImg = (ImageView) convertView.findViewById(R.id.gallery_item_cardimg); 
     convertView.setTag(galleryItem); 
    } 
    else 
    { 
     galleryItem = (GalleryItem) convertView.getTag(); 
    } 

    GTCard gtCard = (GTCard) mRoundDeck.get(position); 
    galleryItem.cardName.setText(gtCard.getNameId()); 
    galleryItem.cardImg.setImageResource(gtCard.getImgId()); 

    return convertView; 
} 

谢谢你的帮助。

塞缪尔

我会建议你使用ViewPager而非画廊。在我个人的经历中,我发现Gallery略有漏洞。如果你的要求是一次只显示一个视图,那么ViewPager(它可以让你左右滑动一个接一个地点)应该满足你的需求。

您将需要包含Android支持包以使用ViewPager。

链接支持包:如何使用ViewPager http://developer.android.com/sdk/compatibility-library.html 链接:http://blog.stylingandroid.com/archives/537

+0

谢谢您的帮助。 ViewPager正是我想要的!该教程工作正常,使用简单。 – Samuel 2012-04-16 06:55:20

+0

不客气。 :) – sparkymat 2012-04-17 12:52:47

你应该使用的TextView和ImageView的两个宽度fillparent.then这将是填补画廊的屏幕...