2014-04-11 1 views
1

다음 코드를 사용하여 instagramAndroid에서 gridview 만들기

의 격자보기를 만들려고합니다.

public class MainActivity extends Activity { 

    static final String[] alphabets = new String[] { 
     "A", "B", "C", "D", "E", 
      "F", "G", "H", "I", "J", 
      "K", "L", "M", "N", "O", 
      "P", "Q", "R", "S", "T", 
      "U", "V", "W", "X"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
        android.R.layout.simple_list_item_1, alphabets); 

      // create a RelativeLayout 
      RelativeLayout relativeLayout = new RelativeLayout(this); 

      // define the RelativeLayout layout parameters. 
      RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.FILL_PARENT, 
        RelativeLayout.LayoutParams.FILL_PARENT); 

      // create a gridview 
      GridView gridView= new GridView(this); 

      gridView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
      gridView.setNumColumns(4); 

      gridView.setMinimumHeight(300); 
      gridView.setMinimumWidth(300); 
      gridView.setColumnWidth(300); 

      gridView.setAdapter(adapter); 
      relativeLayout.setPadding(50, 50, 50, 50); 
      // Adding the gridview to the RelativeLayout as a child 
      relativeLayout.addView(gridView); 

      // set the RelativeLayout as our content view 
      setContentView(relativeLayout, relativeLayoutParams); 



    } 

결과는 내가 높이와 너비를 넣어하지만 여전히 작동하지 않는 노력이 image 같았다. 왜? 직사각형처럼 보입니다. 또한 어떤 아이디어에 테두리를 넣는 방법? 나는이 같은 것을 달성 할 필요가있다.

image1 인 그램에서와 같은 모든 요소에 대해 검은 색과 같은 너비와 열의 경계가 있어야한다. 샘플 및 자습서가 실제로 도움이됩니다.

+0

귀하의 문제점은 무엇입니까? 문자열이나 이미지 만 보여줄 필요가 있습니까? 사용자 지정 어댑터를 사용해 보셨습니까? –

+0

각 요소의 너비와 높이를 제어하는 ​​방법을 알고 싶습니다. 그래서 너비와 높이를 설정하는 사용자 지정 어댑터를 사용해야합니까? – user3487657

+0

그리드 항목에 대한 사용자 정의 레이아웃을 생성해야합니다. – Libin

답변

0

당신은 "grid_view_item라는 이름의 사용자 정의 그리드 항목 레이아웃을 추가

public static class CustomArrayAdapter extends ArrayAdapter<String> { 

     private List<String> strings; 

     /** 
     * 
     * @param context 
     * @param resource 
     * @param objects 
     *   your strings to show 
     */ 
     public CustomArrayAdapter(Context context, int resource, 
       List<java.lang.String> objects) { 
      super(context, resource, objects); 
      // TODO Auto-generated constructor stub 
     } 

     /** 
     * Another constructor that takes Array instead of List 
     * 
     * @param context 
     * @param resource 
     * @param objects 
     *   your strings to show 
     */ 
     public CustomArrayAdapter(Context context, int resource, 
       String[] objects) { 
      super(context, resource, objects); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 

      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View gridItem = inflater.inflate(R.layout.grid_view_item, parent, false); 
      TextView tv = (TextView) gridItem.findViewById(R.id.item); 
      tv.setText((CharSequence) strings.get(position)); 

      return gridItem; 

     } 

    } 

ArrayAdapter와 및 고해상도/레이아웃에서 사용자 정의를 구현하여 MainActivity 클래스에서 정의 ArrayAdapter와 및 사용자 정의 그리드 항목

를 추가해야합니다. xml "

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

     <TextView 
     android:id="@+id/item" 
     android:textColor="#FFDDFF99" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:layout_margin="5dp" 
     android:text="Item" /> 


</LinearLayout> 

필요에 따라 너비 및 높이 특성을 변경할 수 있습니다. 여백 속성을 사용하여 서로 항목을 구분하십시오.