Android 如何将带有图像和文本视图的标题添加到我的导航抽屉中?
我有一个带有部分和项目的导航绘图,但我需要将图像和文本作为标题,但不知道如何准确地从我所拥有的内容,我拥有的导航抽屉是android studio在右键单击并选择“新建>活动>导航抽屉活动”时创建的默认抽屉 这是我想要作为标题的xml: nav_header.xmlAndroid 如何将带有图像和文本视图的标题添加到我的导航抽屉中?,android,navigation-drawer,Android,Navigation Drawer,我有一个带有部分和项目的导航绘图,但我需要将图像和文本作为标题,但不知道如何准确地从我所拥有的内容,我拥有的导航抽屉是android studio在右键单击并选择“新建>活动>导航抽屉活动”时创建的默认抽屉 这是我想要作为标题的xml: nav_header.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Character's Avatar -->
<ImageView android:id="@+id/navigation_drawer_header_iv_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/image_sorcerer"/>
<!-- Character's name -->
<TextView android:id="@+id/navigation_drawer_header_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example text"
android:paddingLeft="16dp"/>
</LinearLayout>
这就是导航现在的样子:
将您的android studio更新为1.4,然后您可以创建一个带有标题图像的导航抽屉活动,这是我正在研究的内容 活动\u main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
android:fitsSystemWindows="true">
<!-- Contenido Principal -->
<include layout="@layout/main_content"
android:id="@+id/mainContent"/>
<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/nav_menu"/>
</android.support.v4.widget.DrawerLayout>
您需要用布局文件替换抽屉标题的地方。
如果要使用NavigationView,必须在渐变中添加此依赖项:
compile 'com.android.support:design:22.2.0'
希望这有帮助。使用
材质设计的导航视图
示例我昨天尝试了这个选项,但没有帮到我,我想我必须向我的自定义适配器添加一些东西,但我不知道它到底是什么。将android studio更新为1.4,并创建带有标题图像和标题的材质滑块的滑块活动滑块活动更容易?创建内部片段导航\u抽屉
package com.jomasa.pocketfantasy.activities;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;
import com.jomasa.pocketfantasy.R;
import com.jomasa.pocketfantasy.fragments.GameInventoryFragment;
import com.jomasa.pocketfantasy.fragments.GameMainFragment;
import com.jomasa.pocketfantasy.fragments.GameNavigationFragment;
import com.jomasa.pocketfantasy.fragments.GameSheetFragment;
public class GameActivity extends AppCompatActivity
implements GameNavigationFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private GameNavigationFragment mGameNavigationFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mGameNavigationFragment = (GameNavigationFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mGameNavigationFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position)
{
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, GameMainFragment.newInstance(position))
.commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, GameInventoryFragment.newInstance(position))
.commit();
break;
case 2:
fragmentManager.beginTransaction()
.replace(R.id.container, GameSheetFragment.newInstance(position))
.commit();
break;
default: break;
}
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.navigation_title_main);
break;
case 2:
mTitle = getString(R.string.navigation_title_inventory);
break;
case 3:
mTitle = getString(R.string.navigation_title_sheet);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mGameNavigationFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.menu_game, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@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);
}
}
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
android:fitsSystemWindows="true">
<!-- Contenido Principal -->
<include layout="@layout/main_content"
android:id="@+id/mainContent"/>
<!-- Navigation Drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/nav_menu"/>
</android.support.v4.widget.DrawerLayout>
app:headerLayout="@layout/drawer_header"
compile 'com.android.support:design:22.2.0'