2017-09-01 46 views
-3

如何在Android菜單項行中插入佈局?如何在Android菜單項行中插入佈局?

我不知道如何創建這樣的佈局。

這是我想要的東西:

+0

我建議你讀了Android佈局教程。 –

+0

謝謝,你能給我一個具體的鏈接嗎? – Adzimzf

+0

http://developer.android.com –

回答

1

創建一個自定義佈局是這樣的:

<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" 
     tools:context=".MainActivity" 
     android:padding="5dp"> 

    <Switch 
     android:id="@+id/mySwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="20dp" 
     android:text="Play with the Switch" /> 

    <TextView 
     android:id="@+id/switchStatus" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/mySwitch" 
     android:layout_marginTop="22dp" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

在你的活動,執行以下代碼:

public void showPopup(View v) { 
    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View popupView = layoutInflater.inflate(R.layout.popup_filter_layout, null); 

    popupWindow = new PopupWindow(
      popupView, 
      ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 

    popupWindow.setBackgroundDrawable(new BitmapDrawable()); 
    popupWindow.setOutsideTouchable(true); 
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 
     @Override 
     public void onDismiss() { 
      //TODO do sth here on dismiss 
     } 
    }); 

    popupWindow.showAsDropDown(v); 
} 
+0

謝謝你的迴應,我會先試一試 – Adzimzf