gpt4 book ai didi

java - 如何将图像标题添加到我的抽屉导航布局中

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:55 27 4
gpt4 key购买 nike

如何将图像标题添加到我的抽屉导航布局中,如下所示。我现在遇到的问题是每个列表项都有自己的图标,但标题图像也打印在每个项目上。

this is what i want enter image description here

我现在拥有的是这样的: this is what i have now enter image description here

mainactivity.java

main activity.java

package be.yvandamme.ginlovers.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.google.android.gms.analytics.GoogleAnalytics;
import be.yvandamme.ginlovers.R;
import be.yvandamme.ginlovers.WebViewAppApplication;
import be.yvandamme.ginlovers.adapter.DrawerAdapter;
import be.yvandamme.ginlovers.fragment.MainFragment;


public class MainActivity extends ActionBarActivity
{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private ListView mDrawerListView;

private CharSequence mTitle;
private CharSequence mDrawerTitle;
private String[] mTitles;


public static Intent newIntent(Context context)
{
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}


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

// init analytics tracker
((WebViewAppApplication) getApplication()).getTracker();
}


@Override
public void onStart()
{
super.onStart();

// analytics
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}


@Override
public void onResume()
{
super.onResume();
}


@Override
public void onPause()
{
super.onPause();
}


@Override
public void onStop()
{
super.onStop();

// analytics
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}


@Override
public void onDestroy()
{
super.onDestroy();
}


@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// action bar menu
return super.onCreateOptionsMenu(menu);
}


@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// open or close the drawer if home button is pressed
if(mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}

// action bar menu behaviour
switch(item.getItemId())
{
case android.R.id.home:
Intent intent = MainActivity.newIntent(this);
startActivity(intent);
return true;

default:
return super.onOptionsItemSelected(item);
}
}


@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}


@Override
public void onConfigurationChanged(Configuration newConfiguration)
{
super.onConfigurationChanged(newConfiguration);
mDrawerToggle.onConfigurationChanged(newConfiguration);
}


@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}


private void setupActionBar()
{
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ActionBar bar = getSupportActionBar();
bar.setDisplayUseLogoEnabled(false);
bar.setDisplayShowTitleEnabled(true);
bar.setDisplayShowHomeEnabled(true);
bar.setDisplayHomeAsUpEnabled(true);
bar.setHomeButtonEnabled(true);
}


private void setupDrawer(Bundle savedInstanceState)
{
mTitle = getTitle();
mDrawerTitle = getTitle();

// title list
mTitles = getResources().getStringArray(R.array.navigation_title_list);

// icon list
TypedArray iconTypedArray = getResources().obtainTypedArray(R.array.navigation_icon_list);
Integer[] icons = new Integer[iconTypedArray.length()];
for(int i=0; i<iconTypedArray.length(); i++)
{
icons[i] = iconTypedArray.getResourceId(i, -1);
}
iconTypedArray.recycle();

// reference
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_layout);
mDrawerListView = (ListView) findViewById(R.id.activity_main_drawer);

// set drawer
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerListView.setAdapter(new DrawerAdapter(this, mTitles, icons));
mDrawerListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View clickedView, int position, long id)
{
selectDrawerItem(position, false);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close)
{
@Override
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}

@Override
public void onDrawerOpened(View drawerView)
{
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

// show initial fragment
if(savedInstanceState == null)
{
selectDrawerItem(0, true);
}
}


private void selectDrawerItem(int position, boolean init)
{
String[] urlList = getResources().getStringArray(R.array.navigation_url_list);
String[] shareList = getResources().getStringArray(R.array.navigation_share_list);

Fragment fragment = MainFragment.newInstance(urlList[position], shareList[position]);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment).commitAllowingStateLoss();

mDrawerListView.setItemChecked(position, true);
if(!init) setTitle(mTitles[position]);
mDrawerLayout.closeDrawer(mDrawerListView);
}
}

抽屉适配器.java

package be.yvandamme.ginlovers.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import be.yvandamme.ginlovers.R;


public class DrawerAdapter extends BaseAdapter
{
private Context mContext;
private String[] mTitleList;
private Integer[] mIconList;


public DrawerAdapter(Context context, String[] titleList, Integer[] iconList)
{
mContext = context;
mTitleList = titleList;
mIconList = iconList;
}


@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// inflate view
View view = convertView;
if(view == null)
{
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.drawer_item, parent, false);

// view holder
ViewHolder holder = new ViewHolder();
holder.titleTextView = (TextView) view.findViewById(R.id.drawer_item_title);
holder.iconImageView = (ImageView) view.findViewById(R.id.drawer_item_icon);
view.setTag(holder);
}

// entity
String title = mTitleList[position];
Integer icon = mIconList[position];

if(title!=null && icon!=null )
{
// view holder
ViewHolder holder = (ViewHolder) view.getTag();

// content
holder.titleTextView.setText(title);
holder.iconImageView.setImageResource(icon);
}

return view;
}


@Override
public int getCount()
{
if(mTitleList!=null) return mTitleList.length;
else return 0;
}


@Override
public Object getItem(int position)
{
if(mTitleList!=null) return mTitleList[position];
else return null;
}


@Override
public long getItemId(int position)
{
return position;
}


public void refill(Context context, String[] titleList, Integer[] iconList)
{
mContext = context;
mTitleList = titleList;
mIconList = iconList;
notifyDataSetChanged();
}


static class ViewHolder
{
TextView titleTextView;
ImageView iconImageView;
}
}

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout
android:id="@+id/activity_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

</FrameLayout>

<ListView
android:id="@+id/activity_main_drawer"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:drawSelectorOnTop="true"
android:fastScrollEnabled="false"
android:listSelector="@drawable/selector_clickable_item_bg"
android:background="@color/global_bg_front"
/>

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

</LinearLayout>

这是我的抽屉项目.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="wrap_content"
android:minHeight="@dimen/global_spacing_l"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="?attr/drawerItemBackground">



<ImageView
android:id="@+id/drawer_image"
android:layout_width="280dp"
android:layout_height="140dp"
android:src="@drawable/loading" />


<ImageView
android:id="@+id/drawer_item_icon"
android:layout_width="@dimen/global_spacing_m"
android:layout_height="@dimen/global_spacing_m"
android:layout_marginLeft="@dimen/global_keyline_s"
android:layout_marginRight="@dimen/global_spacing_xs"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_below="@+id/drawer_image"/>


<TextView
android:id="@+id/drawer_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/global_keyline_s"
android:textAppearance="@style/TextAppearance.WebViewApp.Body1"
android:layout_below="@+id/drawer_image"/>

</RelativeLayout>

最佳答案

尝试将标题 View 添加到 ListView 中(在这种情况下标题也会滚动),您需要在 MainActivity 中执行以下操作:

View header = (View) getLayoutInflater().inflate(
R.layout.header_layout, null);
ImageView img=(ImageView)header.findViewById(R.id.imgView);
img.setBackgroundResource(R.drawable.icon);
mainListView.addHeaderView(header);

或者您可以使用 DesignLibrary,例如 herehere .

关于java - 如何将图像标题添加到我的抽屉导航布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34438785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com