Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin Android抽屉布局-添加图标和分隔线_Android_Android Layout_Xamarin - Fatal编程技术网

Xamarin Android抽屉布局-添加图标和分隔线

Xamarin Android抽屉布局-添加图标和分隔线,android,android-layout,xamarin,Android,Android Layout,Xamarin,我已经在我的Xamarin Android应用程序中成功地创建了一个抽屉布局,但是抽屉目前只是一个纯黑色的布局,其中填充了我的数组中的项目。抽屉项目布局如下所示: <?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/

我已经在我的Xamarin Android应用程序中成功地创建了一个抽屉布局,但是抽屉目前只是一个纯黑色的布局,其中填充了我的数组中的项目。抽屉项目布局如下所示:

<?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>
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);


        }

我在运行时遇到一个错误:

原因:java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.TextView

对于如何将图标添加/声明/分配到我已经声明的字符串数组,我也感到困惑

主活动处理抽屉的方式如下:

<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>
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);


        }
私有MyActionBarDrawerToggle抽屉切换;
私有字符串抽屉;
私有字符串标题;
私有Android.Support.V4.Widget.DrawerLayout抽屉;
私有列表视图抽屉列表;
私有静态只读字符串[]节=
{
“扫描”、“我的报价”、“首选项”、“配置文件”、“暂停扫描”
};
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.NavigationDrawer);
标题=抽屉标题=标题;
抽屉=FindViewById(Resource.Id.drawer\u布局);
抽屉列表=FindViewById(Resource.Id.left\u抽屉);
paurerlist.Adapter=新的ArrayAdapter(此,Resource.Layout.paurerItem,节);
DroperList.ItemClick+=(发件人,参数)=>ListItemClicked(参数位置);
drawer.SetDrawerShadow(Resource.Drawable.drawer\u shadow\u dark,(int)GravityFlags.Start);
抽屉切换=新的MyActionBarDrawerToggle(此,抽屉,
Resource.Drawable.ic\u抽屉灯,
Resource.String.drawer\u打开,
Resource.String.drawer\u close);
//显示当前片段标题并更新选项菜单
抽屉切换。抽屉关闭+=(o,args)=>
{
SupportActionBar.Title=标题;
SupportInvalidateOptions菜单();
};
//显示抽屉标题并更新选项菜单
抽屉切换。抽屉连接+=(o,args)=>
{
SupportActionBar.Title=抽屉;
SupportInvalidateOptions菜单();
};
//将抽屉列表器设置为切换。
抽屉。设置抽屉链接器(抽屉切换);
如果(savedInstanceState==null)
{
ListItemClicked(0);
}
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(真);
}

我已经阅读了关于这个主题的每一篇教程,但我正在努力寻找如何在抽屉项目布局中实现图标和行分隔符而不产生错误。

为此,您的抽屉列表需要一个抽屉指示器

对不起,我正在编辑

您必须为适配器使用此构造函数:

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; }
}
}
新的ArrayAdapter(上下文、R.layout.customview、R.id.textviewid、列表)


所以试试这个:drawerList.Adapter=newarrayadapter(这是Resource.Layout.DrawerItem,R.id.textViewName节)

在该方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方以下内容包括:

使用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; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
命名空间AtlasAndroidApp
{
公共类MenuListAdapter:BaseAdapter
{
活动语境;
公共清单项目;
公共菜单StatAdapter(活动上下文)
:base()
{
this.context=上下文;
this.items=新列表(){
新菜单项(){Name=“بنوششافتی”,Img=Resource.Drawable.icوu action(u store},
新MenuItem(){Name=“دانش㵕نےکک”,Img=Resource.Drawable.ic(action)action(u store},
新MenuItem(){Name=“خوج”,Img=Resource.Drawable.ic\u action\u action\u store}
};
}
公共覆盖整数计数
{
获取{return items.Count;}
}
public override Java.Lang.Object GetItem(int位置)
{
返回位置;
}
公共覆盖长GetItemId(int位置)
{
返回位置;
}
公共覆盖视图GetView(int位置、视图转换视图、视图组父视图)
{
var项目=项目[位置];
变量视图=(convertView??
context.layoutiner.flater.flate(
Resource.Layout.DrawerListItem,
父母亲
假)作为线性布局;
var menuimg=view.findviewbyd(Resource.Id.menuimg)作为ImageView;
var menutxt=view.findviewbyd(Resource.Id.menutxt)作为TextView;
menuimg.SetImageResource(item.Img);
menutxt.SetText(item.Name,TextView.BufferType.Normal);
menutxt.SetTypeface(ToolsClass.XMYEKAN,Android.Graphics.typestyle.Normal);
MenuText.Gravity=GravityFlags.Right;
menutxt.SetTextColor(Android.Graphics.Color.WhiteSmoke);
((LinearLayout)view.findviewbyd(Resource.Id.drawerlinear2)).SetGravity(GravityFlags.Right);
返回视图;
}