私はあなたの助けが必要です:)私はすでに私のアプリを書いているが、今それにナビの引き出しを追加する必要があります。私はnav引き出しを追加したが、どこに私のアイコンを置くべきか分からない。ナビゲーション・ドロワーにアイコンを追加するにはどうすればいいですか?
写真 "1"
pics2 "2"
pics3 "3"
のhierが私のJavaコード セッティングです:私はこのようなナビゲーションアイテムのテキストの左にアイコンを置きたいです。 Javaの
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by vklamm on 31.03.2016.
*/
public class Settings extends AppCompatActivity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
//declarations
//Edittext fields for username , server, password & port information
EditText edtIpurl, edtPort, edtUsername, edtPassword;
//Textviews that can be clicked
TextView databaseDel, databaseRef, magnumgmbh, contact, support;
//imagebuttons for bottom menu
ImageButton contacts, articles, invoices, orders;
//string for server URL
static String serverURL = "http://", port = ":", username = "", password ="";
Thread newSettingsThread;
static boolean hasVisited = false;
SharedPreferences sp;
static String MP = "MyPrefs";
static String APS = "sURL", APP = "sport", APU = "uname", APPass = "pass";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setTitle("Settings");
mPlanetTitles = new String[]{"one", "two", "three"};
mDrawerLayout = (DrawerLayout) findViewById(R.id.dl);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
//mDrawerList.setAdapter(new ArrayAdapter<String>(this,
// R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPlanetTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
// set name if drawer closes
//getSupportActionBar().setTitle("Settings");
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
//set name if drawer opens
//getSupportActionBar().setTitle("Choose options");
//invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerList.bringToFront();
mDrawerLayout.requestLayout();
mDrawerToggle.isDrawerIndicatorEnabled();
}
/*
@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_main, menu);
return true;
}
*/
//
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
Toast.makeText(Settings.this, "Drawer 2",
Toast.LENGTH_SHORT).show();
}
//if u clicked on ||| or <- sign
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
Toast.makeText(Settings.this, "just call under the number +4917620279876 if you see it",
Toast.LENGTH_LONG).show();
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
/**
* Swaps fragments in the main content view
*/
private void selectItem(int position) {
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
//setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
/* set title
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
Toast.makeText(Settings.this, "Drawer 6",
Toast.LENGTH_SHORT).show();
}
*/
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
String positions = parent.getAdapter().getItem(position).toString();
int p = position;
switch (p){
case 0:
Toast.makeText(Settings.this, "0", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(Settings.this, "1", Toast.LENGTH_SHORT).show();
break;
default :
Toast.makeText(Settings.this, "3", Toast.LENGTH_SHORT).show();
}
}
}
}
activity_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- The navigation drawer -->
<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="@color/grayBackground"/>
drawer_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/activated_background"
android:gravity="center_vertical"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#fff" />
描画可能ディレクトリ---> activated_background.xml
<?xml version="1.0" encoding="utf-8"?>
あなたは、その後、独自のアダプタを作成したいと、それは、アイコンとテキストが含まれているように、それの項目のレイアウトを定義し、する必要があります
<item android:drawable="@color/green" android:state_activated="true"/>
<item android:drawable="@color/green" android:state_selected="true"/>
<item android:drawable="@color/green" android:state_pressed="true"/>
<item android:drawable="@color/green" android:state_checked="true"/>
<item android:drawable="@android:color/transparent"/>
といくつかのstrings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="refresh_button">refresh_button</string>
<string name="prev_button">prev</string>
<string name="drawer_open">Open</string>
<string name="drawer_close">Close</string>
</resources>
http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/ –