2013-08-27 93 views

回答

0

你可以把一個彈出式的視圖,或作爲saperate活動甚至(透明backgound,看看Android上的主題選項)

然後,當你想顯示彈出,你可以啓動它從應用程序中的任何活動中,它始終具有與bg和按鈕相同的樣式,您只會將其實際數據饋送給它。

對於〔實施例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="300dp" 
android:background="@drawable/bg_popup" 
android:layout_gravity="center" 
android:alpha="0.8" 
android:visibility="visible" 
android:id="@+id/generic_popup2_container" 
android:layout_height="300dp"> 
<TextView 
    android:id="@+id/generic_popup2_tv_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/white" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="20dp" 
    android:layout_alignParentTop="true" 
    android:text="header" 
    android:gravity="center" 
    android:textSize="28sp" /> 
<TextView 
    android:id="@+id/generic_popup2_tv_inner_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/white" 
    android:text="content" 
    android:textSize="16sp" 
    android:gravity="center" 
    android:layout_marginTop="20dp" 
    android:layout_below="@id/generic_popup2_tv_title" 
    android:layout_centerHorizontal="true" /> 
<LinearLayout 
    android:id="@+id/generic_popup_2_buttons_layout" 
    android:layout_centerHorizontal="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="20dp" 
    android:orientation="horizontal"> 
    <Button 
     android:id="@+id/layout_2_generic_popup_btn_action1" 
     android:layout_width="115dp" 
     android:layout_height="35dp" 
     android:textColor="@color/white" 
     android:layout_marginBottom="10dp" 
     android:textSize="12sp" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/popup_btn_blue" /> 
    <Button 
     android:id="@+id/layout_2_generic_popup_btn_action2" 
     android:layout_width="115dp" 
     android:layout_height="35dp" 
     android:textColor="@color/white" 
     android:textSize="12sp" 
     android:background="@drawable/popup_btn_blue" /> 
</LinearLayout> 

+0

其實我無法預測的TextView的計數,不知何故3,4,5和> 5,這樣我就可以用這種方式爲我的情況? –

+0

這就是通用性可能成爲陷阱的地方,在圖形方面以及如何通過代碼實際構建它以保持通用並且能夠維護它。 我會建議爲每個彈出文件建立一個不同的佈局,只是爲了維護。 –

1

我有同樣的問題來自定義PopupMenu的字體和文字大小。最後我用反射。它可以用於任何自定義彈出菜單項目。 android支持菜單項的資源佈局是在android.support.v7.internal.view.menu.MenuPopupHelper中定義的,它的字段名是「ITEM_LAYOUT」,聲明爲static final;它的值等於「R.layout.abc_popup_menu_item_layout」 我在Grepcode中找到佈局文件並將其複製到我的項目佈局目錄中。我將其命名爲popup_menu_item_layout.xml。我的彈出式菜單項的佈局來這裏

<?xml version="1.0" encoding="utf-8"?> 
<mypackage.PopupMenuItemView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?attr/dropdownListPreferredItemHeight" 
    android:minWidth="196dip" 
    android:paddingRight="16dip"> 

<!-- Icon will be inserted here. --> 

<!-- The title and summary have some gap between them, and this 'group' should be centered vertically. --> 
<RelativeLayout 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_marginLeft="16dip" 
     android:duplicateParentState="true"> 

    <TextView 
      android:id="@+id/title" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:textAppearance="?attr/textAppearanceLargePopupMenu" 
      android:singleLine="true" 
      android:duplicateParentState="true" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal"/> 

    <TextView 
      android:id="@+id/shortcut" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/title" 
      android:layout_alignParentLeft="true" 
      android:textAppearance="?attr/textAppearanceSmallPopupMenu" 
      android:singleLine="true" 
      android:duplicateParentState="true"/> 

</RelativeLayout> 

<!-- Checkbox, and/or radio button will be inserted here. --> 

然後創建自定義類PopupMenuItemView:

public class PopupMenuItemView extends android.support.v7.internal.view.menu.ListMenuItemView { 

public PopupMenuItemView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public PopupMenuItemView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onFinishInflate() { 
    super.onFinishInflate(); 

    applyTypefaceToAll(this, your_typeface); 
    TypefaceUtils.applyTextSizeToAll(this, your_textsize); 
} 

public static void applyTypefaceToAll(View view, Typeface typeface) { 
    if (view instanceof ViewGroup) { 
     ViewGroup viewGroup = (ViewGroup) view; 
     for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++) 
      applyTypefaceToAll(viewGroup.getChildAt(childIndex), typeface); 
    } else if (view instanceof TextView) { 
     TextView textView = (TextView) view; 
     textView.setTypeface(typeface); 
     textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 
    } 
} 

public static void applyTextSizeToAll(View view, float size) { 
    if (view instanceof ViewGroup) { 
     ViewGroup viewGroup = (ViewGroup) view; 
     for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++) 
      applyTextSizeToAll(viewGroup.getChildAt(childIndex), size); 
    } else if (view instanceof TextView) { 
     TextView textView = (TextView) view; 
     textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); 
     textView.setPaintFlags(textView.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 
    } 
} 
} 

最後替換佈局資源ID通過反射菜單項;一些在您的主要活動onCreate方法或在您的應用程序的onCreate方法,其中,如:

try { 
     setFinalStatic(MenuPopupHelper.class.getDeclaredField("ITEM_LAYOUT"), 
       R.layout.popup_menu_item_layout); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    public static void setFinalStatic(Field field, Object newValue) throws Exception { 
    field.setAccessible(true); 

    try { 
     Field modifiersField = Field.class.getDeclaredField("modifiers"); 
     modifiersField.setAccessible(true); 
     modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

    field.set(null, newValue); 
} 
相關問題