如何在导航抽屉中设置自定义图标?

时间:2018-01-30 09:37:52

标签: android icons navigation-drawer

我想在Android导航抽屉中设置自定义PNG图像。但默认情况下它显示为灰色。当我尝试设置自定义图像时,它会自动更改为灰色,但它会着色。

有人能建议我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

在菜单项中添加色调。

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/logout"
    android:iconTint="#464641"
    android:icon="@drawable/ic_exit_to_app_black_24dp"
    android:title="Logout"/>

答案 1 :(得分:0)

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" >

   <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"
            android:textSize="20sp"
            android:text="Country"
            android:background="@drawable/drawer_title_shape" />

        <ListView android:id="@+id/drawer_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="0.1dp"
            android:textColor="@android:color/white"
            android:background="#fff" />

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

drawer_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:layout_height="60dp" >

        <ImageView
            android:id="@+id/flag"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/hello" />

        <TextView
            android:id="@+id/country"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/flag"
            android:layout_centerVertical="true"
            android:textSize="15sp" />

 </RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    String mTitle = "";
    String[] mCountries ;
    int[] mFlags = new int[]{
             // your_image.png
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d,
            R.drawable.d    
    };


    String[] mCount = new String[]{
            "", "", "", "", "",
            "", "", "", "", "" };

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private LinearLayout mDrawer ;
    private List<HashMap<String,String>> mList ;
    private SimpleAdapter mAdapter;
    final private String COUNTRY = "country";
    final private String FLAG = "flag";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mCountries = getResources().getStringArray(R.array.countries);

        mTitle = (String)getTitle();


        mDrawerList = (ListView) findViewById(R.id.drawer_list);

        mDrawer = ( LinearLayout) findViewById(R.id.drawer);


        mList = new ArrayList<HashMap<String,String>>();
        for(int i=0;i<10;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put(COUNTRY, mCountries[i]);
            hm.put(COUNT, mCount[i]);
            mList.add(hm);
        }

        String[] from = { FLAG,COUNTRY};


        int[] to = { R.id.flag , R.id.country};
        mAdapter = new SimpleAdapter(this, mList, R.layout.drawer_layout, from, to);

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        mDrawerList.setAdapter(mAdapter);

    }
相关问题