Android 导航抽屉不显示列表视图。只是空白
Android navigation drawer doesn't show listview. Just blank
我目前正在尝试将导航抽屉添加到我的所有活动中。当我从屏幕边缘滑动时,我可以看到导航抽屉,但里面什么都没有黑色背景这是我的 DrawerActivity
代码
public class DrawerActivity extends Activity {
public DrawerLayout drawerLayout;
public FrameLayout frameLayout;
public String[] mDrawerItems;
public ListView drawerList;
@Override
public void setContentView(int layoutResID) {
drawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
frameLayout = (FrameLayout) drawerLayout.findViewById(R.id.drawer_frame);
drawerList = (ListView) drawerLayout.findViewById(R.id.left_drawer);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(drawerLayout);
mDrawerItems = getResources().getStringArray(R.array.NAV_MENU_ITEMS);
drawerList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mDrawerItems));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_drawer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
和drawer_n_activity.xml
<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">
<FrameLayout
android:id="@+id/drawer_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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="#111"/>
编辑:
和 activitydrawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
我做错了什么?任何帮助将不胜感激
我才意识到问题出在哪里。我将列表视图背景设置为#111,这是不透明的黑色,当然文本也是黑色的,所以看不到。我已将背景设置为#fff(白色),现在可以正常使用了。
我目前正在尝试将导航抽屉添加到我的所有活动中。当我从屏幕边缘滑动时,我可以看到导航抽屉,但里面什么都没有黑色背景这是我的 DrawerActivity
代码public class DrawerActivity extends Activity {
public DrawerLayout drawerLayout;
public FrameLayout frameLayout;
public String[] mDrawerItems;
public ListView drawerList;
@Override
public void setContentView(int layoutResID) {
drawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
frameLayout = (FrameLayout) drawerLayout.findViewById(R.id.drawer_frame);
drawerList = (ListView) drawerLayout.findViewById(R.id.left_drawer);
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(drawerLayout);
mDrawerItems = getResources().getStringArray(R.array.NAV_MENU_ITEMS);
drawerList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mDrawerItems));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_drawer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
和drawer_n_activity.xml
<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">
<FrameLayout
android:id="@+id/drawer_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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="#111"/>
编辑: 和 activitydrawer.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
我做错了什么?任何帮助将不胜感激
我才意识到问题出在哪里。我将列表视图背景设置为#111,这是不透明的黑色,当然文本也是黑色的,所以看不到。我已将背景设置为#fff(白色),现在可以正常使用了。