Android:PopupWindow簡單彈窗改進版

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   使用   io   ar   

Android:PopupWindow簡單彈窗

繼續上一節的內容,改進一下,目標是點擊菜單後把菜單收縮回去並且切換內容,我使用的是PopupWindow+RadioGroup

public class MainActivity extends TabActivity {    private PopupWindow pop;     private TabHost tabhost;    private RadioGroup radiogroup;    private RadioButton tab1,tab2;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.test);                //將layout的xml布局檔案執行個體化為View類對象        LayoutInflater inflater =LayoutInflater.from(this);                View view =inflater.inflate(R.layout.mypop, null);                //建立PopupWindow,參數為顯示對象,寬,高        pop =new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);                //PopupWindow的設定        pop.setBackgroundDrawable(new BitmapDrawable());                //點擊外邊消失        pop.setOutsideTouchable(true);              //設定此參數獲得焦點,否則無法點擊         pop.setFocusable(true);                //設定文本監聽事件        TextView text =(TextView) findViewById(R.id.topmenu);        text.setOnClickListener(new OnClickListener(){            @Override            //判斷是否已經顯示,點擊時如顯示則隱藏,隱藏則顯示            public void onClick(View v) {                if(pop.isShowing()){                    pop.dismiss();                }else{                    pop.showAsDropDown(v);                }                            }                                        });                            //tabhost        tabhost=getTabHost();        tabhost.addTab(tabhost.newTabSpec("a").setContent(R.id.tab1).setIndicator("a"));        tabhost.addTab(tabhost.newTabSpec("b").setContent(R.id.tab2).setIndicator("b"));              //選項        radiogroup = (RadioGroup) view.findViewById(R.id.radiogroup);          //設定radiobutton監聽事件       radioCheckListener l =new radioCheckListener();       radiogroup.setOnCheckedChangeListener(l);    }            //點擊菜單,切換卡並讓菜單消失    public class radioCheckListener implements OnCheckedChangeListener{        @Override        public void onCheckedChanged(RadioGroup group, int checkedId) {            // TODO Auto-generated method stub            switch(checkedId){            case R.id.tabps:                tabhost.setCurrentTab(0);                pop.dismiss();                break;            case R.id.tabhtml:                tabhost.setCurrentTab(1);                pop.dismiss();                break;            }                                            }            }       }

 

菜單布局:

<?xml version="1.0" encoding="utf-8"?>    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="vertical"         android:background="#393C39"         android:padding="10dp"         android:id="@+id/radiogroup"         >                  <RadioButton                         android:id="@+id/tabps"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:text="Photoshop"               android:textColor="#ffffff"               android:checked="true"             />                   <RadioButton               android:id="@+id/tabhtml"             android:layout_width="match_parent"             android:layout_height="match_parent"             android:textColor="#ffffff"             android:text="HTML"             />     </RadioGroup>

主布局:

<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@android:id/tabhost"     ><LinearLayout     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/titlebg"        android:gravity="center"        android:orientation="vertical" >        <TextView            android:id="@+id/topmenu"            android:layout_width="wrap_content"            android:layout_height="46dp"            android:clickable="true"            android:drawableRight="@drawable/ic_menu_trangle_down"            android:gravity="center_vertical"            android:text="全部課程"            android:textColor="#ffffff" />    </LinearLayout>         <TabWidget         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@android:id/tabs"        android:visibility="gone"        >            </TabWidget>        <FrameLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@android:id/tabcontent"        >        <LinearLayout             android:id="@+id/tab1"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical"            android:background="#ff0000"            ></LinearLayout>         <LinearLayout             android:id="@+id/tab2"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical"            android:background="#000000"            ></LinearLayout>    </FrameLayout></LinearLayout></TabHost>

 執行個體下載>>>>>>>>>>>>>>>>>>>>>

 

相關文章:

Android實現下拉導航選擇菜單效果

Android:PopupWindow簡單彈窗改進版

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.