【发布时间】:2017-08-19 11:29:21
【问题描述】:
我创建了一个 Android 应用,并希望在左上角显示一个抽屉。抽屉显示在那里,但当我单击抽屉时列表项不显示。以下是我的代码:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.yihanwang.myapplication.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="@drawable/homepage" />
<TextView
android:id="@+id/topTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="165dp"
android:gravity="center_horizontal"
android:text="HikingMate"
android:textColor="#FEFEFE"
android:textSize="100px"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/subTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/topTextView"
android:layout_alignRight="@+id/topTextView"
android:layout_below="@+id/topTextView"
android:layout_marginEnd="39dp"
android:layout_marginRight="39dp"
android:gravity="center_horizontal"
android:text="Hikers best friend"
android:textColor="#FEFEFE"
android:textSize="50px" />
<Button
android:id="@+id/mapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:background="@drawable/shape"
android:elegantTextHeight="true"
android:text="Plants near me"
android:textColor="#0C640F"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
下面是我的drawer_list_item.xml:
<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/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
下面是我的MainActivity类:
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView topText;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout,
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("Title");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("Title");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mDrawerLayout.addDrawerListener(mDrawerToggle);
button = (Button) findViewById(R.id.mapButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent subscription = new Intent(getApplicationContext(), MapActivity.class);
startActivity(subscription);
}
});
topText = (TextView) findViewById(R.id.topTextView);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}
}
下面是我的列表视图项数组的字符串资源:
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
EDIT1
我重写了下面的函数:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
并删除此行:mDrawerToggle.setDrawerIndicatorEnabled(false);。
现在,当我单击切换时,会显示列表视图项,但切换图标不会更改。下面是截图:
您可以从上面的屏幕截图中看到,左上角的切换按钮是黑色图标,但我设置的是白色图标。我通过mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer); 设置它,如果我删除它似乎这条线不起作用:mDrawerToggle.setDrawerIndicatorEnabled(false);
【问题讨论】:
-
谢谢你的帮助,我把textColor改成了#000,但是没有显示出来
-
我已经尝试了您的建议,并且显示了列表视图项目。但是一旦我删除
mDrawerToggle.setDrawerIndicatorEnabled(false);,切换按钮就会成为默认图标。如果删除此行,如何设置图标? -
好的,谢谢:)
-
感谢您的帮助。你能发布你的答案以便我接受吗?
-
其实这里的答案应该是完全一样的,所以我应该把它标记为重复。不过谢谢。很高兴它有帮助。干杯!
标签: android android-layout listview