现在,问题是,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>
答案 0 :(得分:0)
要使用listview的自定义布局,您必须创建自己的适配器,为此您必须扩展BaseAdapter
,这是一个示例
然后创建自定义适配器的实例,然后将其设置为listview而不是ArrayAdapter
。