【问题标题】:How to add icons to items in the navigation drawer?如何为导航抽屉中的项目添加图标?
【发布时间】:2015-10-26 11:40:21
【问题描述】:

现在,问题是,ArrayAdapter 说,我不能使用drawer_menu.xml,因为它要求资源ID 是TextView。如果是这样,如果我只允许在其中有一个 TextView,我该如何添加一个图标?

drawer_menu.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItem"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#000000"
    android:textSize="16sp"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

MainActivity.java:

    public class MainActivity extends AppCompatActivity {
private String[] mDrawerQuantities = {"Angle", "Area", "Fuel", "Length", "Pressure",
        "Speed", "Temperature", "Time", "Volume", "Weight"};
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_menu, mDrawerQuantities));

}

content_main.xml:

     <!-- The navigation drawer -->
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#ffffff"/>

</LinearLayout>

【问题讨论】:

标签: android navigation icons drawer


【解决方案1】:

要将您的自定义布局用于列表视图,您必须创建自己的适配器,为此您必须扩展 BaseAdapter,这是一个示例

Find here

然后创建自定义适配器的实例,然后将其设置为 listview 而不是 ArrayAdapter

【讨论】:

    猜你喜欢
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多