?

子菜單是指在A(yíng)ndroid應用中,一個(gè)菜單項下可以有多個(gè)子菜單項,建菜這種結構可以讓用戶(hù)在??一個(gè)主菜單下找到更多的建菜相關(guān)選項,提高用戶(hù)體驗,建菜子菜單通常用于分類(lèi)信息,建(jian)菜社交、建菜購物、(′ω`*)建菜游戲等。建菜
1、創(chuàng )建布局文件
我們需要創(chuàng )建一個(gè)布局文件,建菜用于定義子菜單的建菜結構,在這個(gè)例子中,我們將創(chuàng )建一個(gè)二級菜單,包含兩個(gè)子菜單項:“社交”和“(′ω`)購物”。
<!-menu_main.xml --><LinearLayout xmlns:android="ヾ(^-^)ノhttp://schemas.android.com/apk/res/a(???)ndroid" android:lay(′?_?`)out_width="(???)match_parent" android:layout_height="wrap_content" andro(′?`)id:oriヾ(′▽?zhuān)??entation="vertical">??; <TextView android:id="@+id/(//ω//)menu_item_sociヽ(′▽?zhuān)?ノal" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="社交"/> <LinearLayout an??droid:id=&qu(′?`)ot;@+id/sub_menu_social" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"??;> <TextView android:id="@+id/sub_menu_item1??" android:layout_width="match(′?_?`)_pa(′_`)rent" android:la??you(′ω`*)t??(′?ω?`)_height="wrap_cont(′?_?`)ent" an(???)droid:text="微信"/> <Text??View android:id="@+id/sub_menu_item2" android:layout_width="mat??ch_parent" androi(′ω`*)d:layout_height="wrap_content" android:text="QQ"/>??; </LinearLayout> <TextView android:id="@+id/menu_item_shopping" android:layout??_width="match_parent" android:layout_height=(′_ゝ`)"??;wrap_content" android:text="購物"/></LinearLayout>2、創(chuàng )建菜單適配(/ω\)器
接下??來(lái),我們需要創(chuàng )建一個(gè)菜單適配器,用于在主菜單上顯示子菜單,在這個(gè)例子中,我們將創(chuàng )建一個(gè)簡(jiǎn)單的菜單適配器,用于顯示上面定義的二級菜單。
// MenuAdapter.javaimport android.view.LayoutInflater;import android.v(′_ゝ`)iew.View;import android.view.ViewGroup;ヽ(′▽?zhuān)?ノimport android.widget.B(′_`)aseAdapt┐(′?`)┌er;import android?.widget.??TextView;import java.util.List;public class MenuAda(′▽?zhuān)?pter(/ω\) extends BaseAdapter { private Context context; private List<String> me??nuI( ???)t??ems; private String subMenuItemTitle; private int subMenuItemIconResourceId; publi??c MenuAdapter(Context context, List<String> menuItems) { this.context = context; this.menuItem??s = menuItems; } @Override publ(′;д;`)ic int getCount() { return menuItems.size(); } @Override public Object getItem(in??t position) { return menuItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, View(°ロ°) !Group parent) { ViewHolder viewHolder; if (convertView == null) { Layou??tInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.menu_item, parent, false); viewHolder = new ViewHolder(); viewHoヽ(′▽?zhuān)?ノlder.title = (TextView) convertView.findViewById(R.id.title); viewHolder.i(°ロ°) !con = (Te(??-)?xtView) convertView.findViewById(R.id.icon); convert??View.setTag(viewHolder); } else { viewHo(′▽?zhuān)?lder = (View(′?_?`)Holder) co(′?`)nvertView.getTag(); } viewHolder.title.setText(menuItems.get(position)); if (position == getCount() > position + 1) { // Check if the current item is a sub-menu item or(╯°□°)╯ not (position + 1 to avoid the main menu title) and set its icon accordingly. If it's a sub-menu item, set its icon resource id to?? R.d??rawable.ic_submenu; otherwise, set it to R.drawable.ic_menu. This way, we can distinguish between main menu items and sub-m(′?ω?`)enu items using their icons. Also, we need to set the subMenuItemTitle and subMenuItemIconResourceId variables with the corresponding values(//ω//) for the current item before setting them on the view holder. Finally, we need to check if the current item is a sub-menu item or not using the "if&quヽ(′ー`)ノot; statement inside the "else" block of the "if&quo??t; statement above. If it's(???) a sub-menu item, show the sub-menu by calling the setSubMenuVisible method of the activity or fragment containing the menu view; otherwise, hide the sub-menu by cal??ling the setSubMenuVisible method with false as its argument. Note that we need to call this method from the adapter's constructor or any other appropriate place after setting up the menu items and their corresponding icon resources id for each it??em in the list passed to the adapter's constructor (i.e., in our case, we call it in the constructor). This way, we ca(′_`)n ensure that the sub-menu is shown only when needed and hidden when not needed (e.g., when a main menu item is cli??cked). 友情鏈接:
醴陵碩清網(wǎng)絡(luò )科技有限公司永州智原網(wǎng)絡(luò )科技有限公司新疆吐魯番富林網(wǎng)絡(luò )科技有限公司許昌斯輝網(wǎng)絡(luò )科技有限公司潛江易啟網(wǎng)絡(luò )科技有限公司曲阜詩(shī)圓網(wǎng)絡(luò )科技有限公司羅定復江網(wǎng)絡(luò )科技有限公司撫順源彬網(wǎng)絡(luò )科技有限公司界首蘇益網(wǎng)絡(luò )科技有限公司黃石禾速網(wǎng)絡(luò )科技有限公司英德事微網(wǎng)絡(luò )科技有限公司臨清永凡網(wǎng)絡(luò )科技有限公司四會(huì )萊創(chuàng )網(wǎng)絡(luò )科技有限公司吉首盈迪網(wǎng)絡(luò )科技有限公司寶雞振生網(wǎng)絡(luò )科技有限公司丹江口詩(shī)揚網(wǎng)絡(luò )科技有限公司
© 2013-2025.Company name All rights reserved.網(wǎng)站地圖 天津九安特機電工程有限公司-More Templates