我有一个简单的NavigationView
我尝试显示我的个人资料图片。这就是我到目前为止所做的:
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
main_activity.xml:
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_news_feed" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_news_feed">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
<ListView
android:id="@+id/friends_list"
android:layout_weight="7"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
nav_header_news_feed.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/profile_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
facebook:com_facebook_preset_size="small"/>
</LinearLayout>
我很困惑,因为我认为我必须简单地实例化profilePictureView
对象并设置其配置文件ID。我以为我正在做的一切正确,因为第一个名称与登录的用户匹配,我确保用户ID不为空。
编辑:添加了对我的文件的编辑,以反映Mattia Maestrini建议的内容:
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private NavigationView navigationView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_news_feed, null);
navigationView.addHeaderView(header);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
ProfilePictureView profilePictureView = (ProfilePictureView) header.findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
main_activity.xml:
<include layout="@layout/app_bar_news_feed" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
<ListView
android:id="@+id/friends_list"
android:layout_weight="7"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>
答案 0 :(得分:1)
NavigationView
在v23.1.0的支持库中有一个bug。您无法访问HeaderView
方法中onCreate
中包含的观看次数。
解决方法是对HeaderView
进行通知并以编程方式添加它。从app:headerLayout
移除main_activity.xml
,然后像这样更改onCreate
:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_news_feed, null);
navigationView.addHeaderView(header);
ProfilePictureView profilePictureView = (ProfilePictureView) header.findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
编辑:由于您已使用include标记添加标题:
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
您可以在MainActivity.java
中删除标题的自定义通胀:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
而且您不需要在app:headerLayout
中指定main_activity.xml
:
<include
layout="@layout/app_bar_news_feed"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
<ListView
android:id="@+id/friends_list"
android:layout_weight="7"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>