2016-01-13 78 views
-1

我想在我的應用程序中創建一個靜態網格視圖。它應該是5X5尺寸。我知道如何構建它,但我不明白的是,我只想將單個藍色圖像充填到網格的所有行和列中。我只是無法弄清楚如何實現這一點。我認爲,如果我們使用適配器,我們應該有一個圖片列表左右,但我只有一個圖像。創建一個靜態網格視圖

+0

要在網格視圖顯示單個圖像或多個 – KCN

+0

難道你不想想[網格佈局(HTTP://開發商。 android.com/reference/android/widget/GridLayout.html)? – Blunderer

+0

是所有行和列中的單個圖像 –

回答

1

如果你只是想呈現相同的圖像機智5×5

MainActivity

gv=(GridView) findViewById(R.id.gridView1); 
gv.setAdapter(new CustomAdapter(this)); 
創建activity_main.xml中

<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" 
    android:orientation="vertical" 

    tools:context=".MainActivity" > 

    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView1" 
     android:numColumns="5" > 

    </GridView> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="25dp" 
     android:textStyle="bold" 
     android:text=" Computer Languages..." /> 

</RelativeLayout> 

通話

創建CustomAdapter。

public class CustomAdapter extends BaseAdapter { 
    int count = 10; 
    Context context; 
    private static LayoutInflater inflater=null; 
    public CustomAdapter(MainActivity mainActivity) { 
     // TODO Auto-generated constructor stub 
     context=mainActivity; 
     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return count; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 
    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View rowView = inflater.inflate(R.layout.image_list, null); 
     return rowView; 
    } 

} 

,爲此佈局,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_gravity="center" 
     android:layout_width="88dp" 
     android:layout_height="88dp" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:src="@drawable/ic_launcher" /> 

</LinearLayout> 
+0

這正是我想要的。謝謝 –

+0

但我有一個問題,當我設置grid_item的寬度說50 dp它在網格項目 –

+0

設置屬性的圖像視圖之間留出空格android android:id =「@ + id/imageView1」 android:layout_gravity =「center」 android:layout_width =「match_parent」 android:layout_height =「match_parent」 android:src =「@ drawable/ic_launcher」/> – 2016-01-13 11:08:30