我不知道如何将设备中的图像加载到CircleImageView中。我的代码nav_header_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/background_material_red"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="@drawable/background_material_red"
app:civ_border_width="2dp"
app:civ_border_color="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:onClick="chooseImage" />
<com.devspark.robototextview.widget.RobotoTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/userName"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="start"
android:paddingBottom="4dp"
android:id="@+id/userName"
app:typeface="roboto_medium"
android:layout_below="@+id/profile_image"
android:layout_alignStart="@+id/profile_image"
android:layout_marginTop="3dp" />
<com.devspark.robototextview.widget.RobotoTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/userEmail"
android:id="@+id/userEmail"
android:gravity="start"
android:textSize="14sp"
android:textColor="#FFF"
app:typeface="roboto_regular"
android:layout_below="@+id/userName"
android:layout_alignStart="@+id/userName"
android:layout_marginTop="3dp" />
</RelativeLayout>
和MainActivity代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private NavigationView navigationView = null;
private Toolbar toolbar = null;
private MainFragment mainFragment;
private GalleryFragment galleryFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainFragment = new MainFragment();
galleryFragment = new GalleryFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainFragment);
fragmentTransaction.commit();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
if (drawer != null) {
drawer.addDrawerListener(toggle);
}
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
if (navigationView != null) {
navigationView.setNavigationItemSelectedListener(this);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_main) {
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mainFragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_health) {
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, galleryFragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer != null) {
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
}
我将重复我的问题:如何通过编程将图像从设备加载到nav_header_main.xml文件
这是一个例子。试试看!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/
http://tjkannan.blogspot.in/2012/01/load-image-from-camera-or-gallery.html