色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Android DrawLayout結合ListView用法實例

瀏覽:17日期:2022-09-22 13:45:51

想做一個APP,設計中有側邊欄這個功能,所以現在開始學習下側邊欄的實現。

在官方的UI空間中已經給出了DrawerLayout這個側滑的菜單空間。

因為在使用DrawerLayout的時候遇到了些問題,花了一天是時間才搞定,這里來記錄一下,免得到時候自己在掉坑里。

1.主布局一定要是DrawerLayout。

2.側欄拉出來時,要點擊空白欄關閉側欄的話,一定要把空白欄設置為FrameLayout

先上個效果圖吧:

Android DrawLayout結合ListView用法實例

好了,上代碼:

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' xmlns:app='http://schemas.android.com/apk/res-auto' android: android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.MainActivity'> <!-- 內容欄--> <FrameLayout android: android:layout_width='match_parent' android:layout_height='match_parent' /> <!-- 側滑欄--> <ListView android: android:layout_width='300dp' android:layout_height='match_parent' android:layout_gravity='start' android:background='#fff000' android:choiceMode='singleChoice' android:divider='#FFFFFF' android:dividerHeight='1dp' /></android.support.v4.widget.DrawerLayout>

listView里面的布局 item_list.xml

<?xml version='1.0' encoding='utf-8'?><LinearLayout 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' xmlns:app='http://schemas.android.com/apk/res-auto'> <!-- 定義一個用于顯示頭像的ImageView --> <ImageView android: android:layout_width='64dp' android:layout_height='64dp' android:baselineAlignBottom='true' android:paddingLeft='8dp' /> <!-- 定義一個豎直方向的LinearLayout,把QQ呢稱與說說的文本框設置出來 --> <LinearLayout android: android:layout_width='match_parent' android:layout_height='wrap_content' android:orientation='vertical'> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingLeft='8px' android:textColor='#1D1D1C' android:textSize='20sp' /> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingLeft='8px' android:textColor='#B4B4B9' android:textSize='14sp' /> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:paddingLeft='8px' android:textColor='#B4B4B9' android:textSize='14sp' /> </LinearLayout></LinearLayout>

主程序MainActivity.java

package action.sun.com.testdraw2;import android.support.v4.app.FragmentManager;import android.support.v4.widget.DrawerLayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{ private String[] names = new String[]{'Tom', 'Jack', 'Json'}; private String[] says = new String[]{'111111,2222222', '33333333~', '444444444~'}; private String[] times = new String[]{'1天前', '3天前~', '2天前~'}; private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher}; private DrawerLayout drawer_layout; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d('1', 'onCreate: xxxxxxxxxxxxxxx'); drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout); List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); for (int i = 0; i < names.length; i++) { Map<String, Object> showitem = new HashMap<String, Object>(); showitem.put('touxiang', imgIds[i]); showitem.put('name', names[i]); showitem.put('says', says[i]); showitem.put('time', times[i]); listitem.add(showitem); } //創建一個simpleAdapter SimpleAdapter myAdapter = new SimpleAdapter(getApplicationContext(), listitem,R.layout.item_list, new String[]{'touxiang', 'name', 'says','time'},new int[]{R.id.imgtou, R.id.name, R.id.says, R.id.time}); //ListView 容器 listView = (ListView) findViewById(R.id.list_left_drawer); listView.setAdapter(myAdapter); listView.setOnItemClickListener(this); } //點擊Item 顯示在幀頁面選擇的Item值 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, '提示的內容', Toast.LENGTH_LONG).show(); //關閉 側邊欄 drawer_layout.closeDrawer(listView); }}

到了現在,代碼完了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 久草在线在线观看 | 欧美特黄三级成人 | 亚洲欧美日韩中文字幕在线一区 | 成人国产在线看不卡 | 成人手机在线视频 | aa毛片免费全部播放完整 | 亚洲一区在线播放 | 免费岛国小视频在线观看 | 拍拍拍又黄又爽无挡视频免费 | 久久精品国产精品亚洲综合 | 国产日韩欧美综合在线 | 国产90后美女露脸在线观看 | 免费一级欧美毛片 | 国产精品情人露脸在线观看 | 长腿美女被啪的欲仙欲死视频 | 欧美成人怡红院在线观看 | 久久久久国产一级毛片高清片 | 国产日韩在线视频 | 欧美日韩国产一区二区三区在线观看 | 黄页网址免费观看18网站 | 波多野结衣在线观看一区 | 香蕉香蕉国产片一级一级毛片 | 久久久久久毛片免费观看 | 国产精品1区2区3区 国产精品1区2区3区在线播放 | 香蕉国产人午夜视频在线 | 中文字幕在线播放 | 韩国一级理黄论片 | 日韩在线视频中文字幕 | 亚洲aⅴ男人的天堂在线观看 | 免费在线看黄网址 | 在线看a级片 | 久久精品国产只有精品2020 | 九九在线精品视频xxx | 五月久久亚洲七七综合中文网 | 亚洲一区不卡 | 日韩精品一区二区在线观看 | 欧美成人在线免费观看 | 国产伦精品一区三区视频 | 97在线免费视频 | 美女久草 | 国产精品黄网站免费进入 |