標籤: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簡單彈窗改進版