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

沙希·帕特尔(Shashi Patel)

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

谁能建议我解决这个问题的方法?

苛刻

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>

抽屉式布局.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);

    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中创建自定义导航抽屉

如何在android中制作自定义导航抽屉

如何在导航抽屉中居中自定义布局?

如何在抽屉导航器中添加自定义抽屉,反应导航4.x?

无法在自定义导航抽屉中设置按钮的text

如何在Express中设置自定义图标?

如何创建自定义导航抽屉?

反应导航中的自定义抽屉

将自定义图标添加到抽屉导航

添加自定义导航抽屉操作栏图标

React Native 抽屉导航:如何通过单击特定场景中的自定义按钮打开抽屉?

打开/关闭抽屉上的Android导航抽屉自定义图标

如何在Leaflet中定义多个自定义图标?

如何在Wordpress中的自定义插件上设置图标

如何在 mac osx 中为文件类型设置自定义图标

如何在Flutter中设置自定义图标Google Map

如何在导航抽屉中设置SettingsActivity?

如何自定义导航抽屉项目高度

自定义导航抽屉中setOnClickListener的最佳做法

如何创建自定义导航抽屉,该抽屉将在抽屉onitemclick旁边打开ListView?

如何使用基于抽屉导航的自定义组件从侧边菜单中获取价值

如何更改导航抽屉布局中的设置图标?

如何在UWP的NavigationViewMenuItems中添加自定义图标

如何在占位符中插入自定义图标?

如何在mvvmlight中实现自定义导航服务

如何在NativeScript中创建自定义导航栏?

如何在导航栏中包含自定义开关

如何在swiftui中自定义导航栏

反应本机自定义抽屉导航