我已经在我的Xamarin Android应用程序中成功创建了一个drawerlayout,但抽屉目前只是一个简单的黑色布局,其中填充了我的数组中的项目。抽屉项目布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:singleLine="true"
android:background="@drawable/activated_background"
android:minHeight="?android:attr/listPreferredItemHeight" />
我想通过添加图标和行分隔符使抽屉更漂亮,但我无法找到方法来做到这一点。如果我将ImageView添加到抽屉项目的axml布局以允许添加图标,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="10dp" >
<ImageView
android:id="@+id/imageViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="10dp" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageViewIcon"
android:paddingRight="10dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#ffffff" />
</RelativeLayout>
我在运行时收到错误:
引起:java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.TextView
我也很困惑如何添加/声明/将图标分配给我已经声明的字符串数组。
主活动像这样处理drawerLoyout:
private MyActionBarDrawerToggle drawerToggle;
private string drawerTitle;
private string title;
private Android.Support.V4.Widget.DrawerLayout drawer;
private ListView drawerList;
private static readonly string[] Sections =
{
"Scan", "My Offers", "Preferences", "Profile", "Pause Scan"
};
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.NavigationDrawer);
title = drawerTitle = Title;
drawer = FindViewById<Android.Support.V4.Widget.DrawerLayout>(Resource.Id.drawer_layout);
drawerList = FindViewById<ListView>(Resource.Id.left_drawer);
drawerList.Adapter = new ArrayAdapter<string>(this, Resource.Layout.DrawerItem, Sections);
drawerList.ItemClick += (sender, args) => ListItemClicked(args.Position);
drawer.SetDrawerShadow(Resource.Drawable.drawer_shadow_dark, (int)GravityFlags.Start);
drawerToggle = new MyActionBarDrawerToggle(this, drawer,
Resource.Drawable.ic_drawer_light,
Resource.String.drawer_open,
Resource.String.drawer_close);
//Display the current fragments title and update the options menu
drawerToggle.DrawerClosed += (o, args) =>
{
SupportActionBar.Title = title;
SupportInvalidateOptionsMenu();
};
//Display the drawer title and update the options menu
drawerToggle.DrawerOpened += (o, args) =>
{
SupportActionBar.Title = drawerTitle;
SupportInvalidateOptionsMenu();
};
//Set the drawer lister to be the toggle.
drawer.SetDrawerListener(drawerToggle);
if (savedInstanceState == null)
{
ListItemClicked(0);
}
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);
}
我已经阅读了关于这个主题的每个教程,但我很难找到如何在抽屉项目布局中实现图标和行分隔符而不会产生错误。
答案 0 :(得分:0)
你的抽屉列表需要一个DrawerAdapter
抱歉,我试图编辑你必须为你的适配器使用这个构造函数:
new ArrayAdapter(context,R.layout.customview,R.id.textviewid,List);
所以试试这个:drawerList.Adapter = new ArrayAdapter(this,Resource.Layout.DrawerItem,R.id.textViewName Sections);
答案 1 :(得分:0)
نحوهایجادمنویکشوییبهوسیلهمونو(زامارینوسیشارپ!)برایزبانهاییمثلفارسیوعربیکهازراستبهچپهستند:
使用xamarin.android为波斯语和阿拉伯语等语言从右到左制作抽屉布局:
您需要将.axml文件作为ListItem(将添加到listview的行)和适配器,以便:
drawerListItem.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget28"
android:layout_width="match_parent"
android:layout_height="67dp"
android:background="#eeeeee">
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:id="@+id/relativeLayout1"
android:layout_marginBottom="5dp">
<TextView
android:id="@+id/textTop1"
android:layout_width="fill_parent"
android:background="@drawable/textview_background"
android:layout_height="62dp" />
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:background="@drawable/textview_background1"
android:id="@+id/linearLayout1">
<RelativeLayout
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:id="@+id/linearLayout4"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:layout_marginRight="0dp">
<TextView
android:id="@+id/textTop"
android:layout_width="match_parent"
android:textColor="#fff"
android:textSize="20dp"
android:layout_marginRight="10dp"
android:gravity="right"
android:layout_weight="2"
android:text="asdasd"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_centerVertical="true" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
然后你的适配器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace AtlasAndroidApp
{
public class MenuListAdapter : BaseAdapter
{
Activity context;
public List<MenuItem> items;
public MenuListAdapter(Activity context)
: base()
{
this.context = context;
this.items = new List<MenuItem>() {
new MenuItem() { Name = "بهنود شرافتی", Img =Resource.Drawable.ic_action_action_store },
new MenuItem() { Name = "دانش آفرین بی کران", Img = Resource.Drawable.ic_action_action_store },
new MenuItem() { Name = "خروج", Img = Resource.Drawable.ic_action_action_store }
};
}
public override int Count
{
get { return items.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
var view = (convertView ??
context.LayoutInflater.Inflate(
Resource.Layout.DrawerListItem,
parent,
false)) as LinearLayout;
var menuimg = view.FindViewById(Resource.Id.menuimg) as ImageView;
var menutxt = view.FindViewById(Resource.Id.menutxt) as TextView;
menuimg.SetImageResource(item.Img);
menutxt.SetText(item.Name, TextView.BufferType.Normal);
menutxt.SetTypeface(ToolsClass.XMYEKAN, Android.Graphics.TypefaceStyle.Normal);
menutxt.Gravity = GravityFlags.Right;
menutxt.SetTextColor(Android.Graphics.Color.WhiteSmoke);
((LinearLayout)view.FindViewById<LinearLayout>(Resource.Id.drawerlinear2)).SetGravity(GravityFlags.Right);
return view;
}
}
public class MenuItem
{
public MenuItem()
{
}
public MenuItem(string name, int img)
{
this.Name = name;
this.Img = img;
}
public string Name { get; set; }
public int Img { get; set; }
}
}
最后你可以在你的表现层使用它:
private ListView _drawerList= FindViewById<ListView>(Resource.Id.GroupsActivityLayout_right_drawer);
_drawerList.Adapter = new MenuListAdapter(this);
并且您的活动布局应该是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GroupsActivityLayout_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right">
<!-- The main content view -->
<FrameLayout
android:id="@+id/GroupsActivityLayout_content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/GroupsActivityLayout_right_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="2dp"
android:background="#191919" />
</android.support.v4.widget.DrawerLayout>
你将拥有这样的DrawerLayout: