2010-08-21 30 views
7

Đối với ứng dụng tôi đang tạo, tôi dự định có một menu chính gồm 6 biểu tượng khác nhau, với 2 biểu tượng trên mỗi dòng. Điều này rất giống với bố cục menu chính của Twitter được thấy ở đây: nothxCách thiết lập bố cục menu chính trong Android?

Vì vậy, về cơ bản ... tôi nên thiết lập XML như thế nào? LinearLayout, TableLayout? Và sau đó, những gì tôi thực sự làm để có được các biểu tượng và văn bản được khoảng cách đều nhau và như vậy? Tôi đã thử tất cả mọi thứ tôi có thể nghĩ đến cho đến nay và vô ích.

+1

Tại sao không thử một cái nhìn lưới? Có kiểu xem này ở đây, tôi nghĩ rằng điều này có thể giúp kết hợp với một số tư duy sáng tạo khác .. Tôi cho rằng họ có thể nhấp vào quá nên tất cả những gì bạn phải làm là biến nó thành trường hợp chuyển đổi hoặc gì đó. – Shouvik

+0

Vui lòng chấp nhận câu trả lời nếu bạn cho rằng câu trả lời đúng. Nó sẽ giúp những người khác đối mặt với cùng một vấn đề tìm giải pháp nhanh hơn. –

Trả lời

7

Có sử dụng GridView & TextView (với CompoundDrawables) - Tôi đã làm điều này trước:

main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <GridView android:id="@+id/grid" android:numColumns="2" 
     android:horizontalSpacing="20dip" android:verticalSpacing="20dip" 
     android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
</LinearLayout> 

MainActivity:

GridView grid = (GridView) findViewById(R.id.grid); 
     grid.setAdapter(new HomeScreenShortcutAdapter()); 
     grid.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View v, int position, 
        long id) { 

       startActivity(i); // Specify activity through Intent i 
      } 
     }); 

public class HomeScreenShortcutAdapter extends BaseAdapter { 



     HomeScreenShortcutAdapter() { 

     } 

     @Override 
     public int getCount() { 
      return 0; 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      TextView tv; 
      final Object data = getItem(position); 

      if (convertView == null) { 

       tv = new TextView(getApplicationContext()); 
       tv.setGravity(Gravity.CENTER); 

      } else { 
       tv = (TextView) convertView; 
      } 

      Drawable icon = data.icon; 
      CharSequence title = data.title; 

      tv.setCompoundDrawablesWithIntrinsicBounds(
        null, icon, null, null); 
      tv.setText(title); 
      tv.setTag(data); 

      return tv; 
     } 

    } 
Các vấn đề liên quan