我有一个 400 x 206 图像,我试图在抽屉导航的菜单下方显示该图像,但当我打开抽屉导航时它没有显示。奇怪的是,在 AS 设计预览中,图像看起来像是被转换成一条线。我现在还发现我打开的抽屉在最右边有一条额外的透明空间,大约半英寸宽。我认为两者是相关的,但我当前的首要任务是弄清楚如何让图像显示在菜单项下方。
activity_main.xml 的 Android Studio 设计预览。一直向右延伸的顶部蓝色轮廓是 left_drawer,其中不完全向右的对象是 left_drawer_listview,下面的行是抽屉_buttom_image:
作为记录,我查看了 Background image and image view in Navigation Drawer这要复杂得多,所以我在根据我的需求简化解决方案时可能犯了一个错误。
activity_main.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">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. -->
<LinearLayout
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical">
<ListView
android:id="@+id/left_drawer_listview"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="@color/secondary_blue_transparent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
<ImageView
android:id="@+id/drawer_bottom_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/testimage"
android:contentDescription="@string/testdescr"></ImageView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
drawer_list_item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/drawer_icon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/selector"
android:contentDescription="@string/app_name"
android:paddingLeft="10dp"
android:paddingRight="5dp" />
<TextView
android:id="@+id/drawer_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/drawer_icon"
android:background="@drawable/selector"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#fff" />
</RelativeLayout>
主要 Activity 代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataList = new ArrayList<DrawerItem>();
mTitle = mDrawerTitle = getTitle();
mDrawerTitles = getResources().getStringArray(R.array.drawer_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
mDrawerListView = (ListView) findViewById(R.id.left_drawer_listview);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
for (int i = 0; i < mDrawerTitles.length; ++i) {
dataList.add(new DrawerItem(mDrawerTitles[i], mDrawerIcons[i]));
}
// set up the drawer's list view with items and click listener
drawerAdapter = new CustomDrawerAdapter(this, R.layout.drawer_list_item,
dataList);
mDrawerListView.setAdapter(drawerAdapter);
/* mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerTitles));*/
mDrawerListView.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
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(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(1);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, do x
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
//Fragment fragment = new PlanetFragment();
Fragment fragment = new EventFragment();
Bundle args = new Bundle();
/* // Pass in variables
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);*/
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerListView.setItemChecked(position, true);
//setTitle(mDrawerTitles[position]);
mDrawerLayout.closeDrawer(mDrawerLinear);
}
最佳答案
这是因为 ListView
已经使用了所有可用高度。将 ListView
高度更改为 0dp
并添加权重 1
。这会导致它使用除已使用的高度之外的所有可用高度。
<ListView
android:id="@+id/left_drawer_listview"
android:layout_width="240dp"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/secondary_blue_transparent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
关于java - ImageView 未出现在抽屉导航中的 ListView 下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25693106/