网格视图不平衡问题(图像隐藏一些文字)

时间:2013-12-31 19:13:23

标签: android

http://upload.lsforum.net/users/public/h10504testj63.jpg

我想在我的应用程序的主菜单上显示11个图标,但现在我遇到了麻烦。 在第1行,I​​tem3太长,因此第2行为显示Item3提供了一些位置。 在第2行,Item4也太长,但第3行没有为显示Item4提供一些位置。

我尝试了我的代码,activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical" > 

<GridView   
    android:id="@+id/MenuItemGrid"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"
    android:stretchMode="spacingWidthUniform"
    android:columnWidth="88dp"
    android:gravity="center"
    android:layout_gravity="center" 
    android:numColumns="3" />

</LinearLayout> 

这是我的主要活动

public class MainActivity extends Activity {
    private int[] ItemImage = { R.drawable.ic_launcher, R.drawable.ic_launcher,                R.drawable.ic_launcher,  
        R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, 
        R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, 
        R.drawable.ic_launcher ,R.drawable.ic_launcher };  

    private String[] ItemText = {   "Test Long Item 1", "Test Long Item 2", "Test Long Item 333333333333", 
            "Test Long Item 4444444444444", "Test Long Item 5", "Test Long Item 6",
            "Test Long Item 7", "Test Long Item 8", "Test Long Item 9",
            "Test Long Item 10", "Test Long Item 11"};  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView MenuItemGrid = (GridView) findViewById(R.id.MenuItemGrid);  

        ArrayList<HashMap<String, Object>> ItemList = new ArrayList<HashMap<String, Object>>();  

        for (int i = 0; i < ItemImage.length; i++) {  
            HashMap<String, Object> ItemMap = new HashMap<String, Object>();  
            ItemMap.put("ItemImage", ItemImage[i]);  
            ItemMap.put("ItemText", ItemText[i]);  
            ItemList.add(ItemMap);  
        } 

        SimpleAdapter simpleAdapter = new SimpleAdapter(this, ItemList,  
        R.layout.items, new String[] { "ItemImage", "ItemText" }, new int[] {  
        R.id.image, R.id.title });  

        MenuItemGrid.setAdapter(simpleAdapter); 
    }

}

items.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"     
    android:gravity="center_horizontal"     
    android:orientation="vertical" >  

<ImageView  
    android:id="@+id/image"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />  

<TextView  
    android:id="@+id/title"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"
    android:layout_below="@+id/image"
    android:gravity="center_horizontal"
    android:layout_centerHorizontal="true" />  

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

使用android:stretchMode =“columnWidth”