如何在NavigationView的右侧添加图标

时间:2018-12-26 06:58:32

标签: xamarin.android navigationview

我正在使用Xamarin.android应用程序,其中包含带有NavigationView的Drawer布局。我需要将图标更改为textView的右侧,而不是左侧。

这是我的布局代码:

 <android.support.design.widget.NavigationView
  android:id="@+id/questiontypes_nav_view"
  android:layout_width="220dp"
  android:ellipsize="end"
    android:layout_below="@+id/navigation_drawer_top"
  android:maxLines="1"
    android:scrollbars="none"
      android:layout_above="@+id/navigation_drawer_bottom"
    android:nestedScrollingEnabled="true"
      android:layoutDirection="ltr"
  android:layout_height="match_parent"
    app:theme="@style/NavigationDrawerStyle"
  android:background="#031d2c"        
  android:fitsSystemWindows="true"
  app:itemTextAppearance="?android:attr/textAppearanceSmall"
  app:itemTextColor="@android:color/white"
  app:itemBackground="@drawable/nav_item_drawable"
  app:itemIconTint="@drawable/nav_item_drawable">

  </android.support.design.widget.NavigationView>

有什么方法可以将图标显示在NavigationView的右侧。

1 个答案:

答案 0 :(得分:1)

根据您的描述,是否要在textview和icon之间切换位置?如果是这样,您可以创建一个列表视图以在NavigationView中实现它,如以下代码所示。

这是演示的GIF。

enter image description here

string_intrare = [['Gheorghe', 'Gita', '8', '7', '5.5', '10'],
['Vuia', 'Vasile', '4', '10', '10', '10'],
['Andreescu', 'Andra', '9', '10', '9', '10'],
['Elenescu', 'Elena', '5', '5', '5', '5']]
n = 4


for i in range(0,n):
    if (float(string_intrare[i][2]) < 5) or (float(string_intrare[i][3]) < 5) or (float(string_intrare[i][4]) < 5) or (float(string_intrare[i][5]) < 5):
        del string_intrare[i]
        print('List ' + str(i) + ' has been removed remove' + ' ===== Grade lower than 5')

Traceback (most recent call last):
  File ".\note_bacalaureat.py", line 16, in <module>
    if (float(string_intrare[i][2]) < 5) or (float(string_intrare[i][3]) < 5) or (float(string_intrare[i][4]) < 5) or (float(string_intrare[i][5]) < 5):
IndexError: list index out of range

ListItem.axml(自定义菜单)

$field1 = array_column($objarr, "field1");
// Now you have a flat array you can use in_array or array_search on.
$key = array_search(1150, $field1);
if($key !== false){
    echo "found at key " . $key;
}

这是<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="220dp" android:scrollbars="none" android:nestedScrollingEnabled="true" android:layoutDirection="ltr" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main"> <ListView android:paddingTop="500px" android:id="@+id/lst_menu_items" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout> 方法。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Ls_textview"
        android:text="this is text"
        android:textSize="15dp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="18dp"/>
    <ImageView
        android:layout_marginLeft="50dp"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/Ls_ImageView"
        android:src="@drawable/lcon"/>

</LinearLayout>

您可以像以下代码一样处理click事件。

OnCreate()

这是列表视图的适配器

        ListView listView = FindViewById<ListView>(Resource.Id.lst_menu_items);
        List<ListItem> listItems = new List<ListItem>();
        listItems.Add(new ListItem(Resource.Drawable.lcon, "Title1"));
        listItems.Add(new ListItem(Resource.Drawable.lcon, "Title2"));
        listItems.Add(new ListItem(Resource.Drawable.lcon, "Title3"));
        listItems.Add(new ListItem(Resource.Drawable.lcon, "Title4"));
        MyAdapter myAdapter=  new MyAdapter(listItems, this);
        listView.Adapter = myAdapter;
        listView.ItemClick += (s, e) =>
        {
            OnClick(e.Position);
        };