2011-11-26 37 views
0

可能重複:
SubMenu Layout Android自定義SubMenu Android - 任何幫助?

反正是有作出以下子菜單看起來像第二(Photoshop處理)的形象?

目前,這是我的子菜單: enter image description here

這裏是XML文件,它:

<menu> 
    <item android:id="@+id/facebook_icon" 
     android:title="Share With Facebook" 
     android:icon="@drawable/ic_menu_facebook" /> 

    <item android:id="@+id/twitter_icon" 
     android:title="Share With Twitter" 
     android:icon="@drawable/ic_menu_twitter" />   
</menu> 

我希望它看起來像這樣(如果你有任何想法,我將不勝感激聽到他們! ): enter image description here

回答

1

是的,這是可能的。嘗試一些事情是這樣的: 1.創建類來存儲您的項目類型

public final class AccountTypesProvider { 
    public static List<AccountType> accountTypes = Collections.unmodifiableList(Arrays.asList(
     new AccountType(AccountType.TWITTER_ACCOUNT, "Twitter", R.drawable.ic_menu_twitter), 
     new AccountType(AccountType.FACEBOOK_ACCOUNT, "Facebook", R.drawable.ic_menu_facebook)  
)); 
} 

2.創建ListAdapter

public final class AccountsTypesListAdapter extends ArrayAdapter<AccountType> { 
    private Activity context; 
    private List<AccountType> accountTypes; 
    public AccountsTypesListAdapter(Activity context, List<AccountType> accountTypes) { 
    super(context, R.layout.select_account_item, accountTypes); 
    this.context = context; 
    this.accountTypes = accountTypes; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    View row = inflater.inflate(R.layout.select_account_item, parent, false);  
    TextView label = (TextView) row.findViewById(R.id.text_item); 
    label.setText(accountTypes.get(position).title);  
    ImageView icon = (ImageView) row.findViewById(R.id.icon_item); 
    icon.setImageResource(accountTypes.get(position).bigIconId); 
    return row; 
    } 
} 

3.Layout您的適配器:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10px">  <ImageView android:id="@+id/icon_item" android:layout_width="wrap_content" 
       android:layout_height="fill_parent"/> 
       <TextView android:id="@+id/text_item" android:layout_width="wrap_content" 
        android:layout_height="fill_parent" android:paddingLeft="10px" 
        android:paddingTop="5px" android:textStyle="bold" 
        android:textColor="#000000"/> 
     </LinearLayout> 

4。而你的對話

public static void showSelectAccountTypeDialog(Activity context, String title, OnClickListener dialogListener) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setTitle(title); 
    builder.setAdapter(new AccountsTypesListAdapter(context, AccountTypesProvider.accountTypes), dialogListener); 
    builder.create().show(); 
    } 

此代碼摘自here

0

壞消息是上下文菜單不支持圖標。

好消息是你可以做些什麼,但它需要更多的工作。您可以按照this article解釋所需的步驟,或者您可以採取簡單的方法並使用類似this one的圖書館。