我想在下面的网格视图中创建带图像的按钮是我的代码:
My FragmentHome.java:
package com.erlanggastudio.myagri.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.erlanggastudio.myagri.R;
import com.erlanggastudio.myagri.adapters.AdapaterGridView;
import com.erlanggastudio.myagri.adapters.AdapterGridViewItem;
import com.erlanggastudio.myagri.adapters.AdapterImageSlider;
import java.util.ArrayList;
public class FragmentHome extends Fragment implements ViewPager.OnPageChangeListener {
//For Page Viewer Dots
//private ImageButton btnNext, btnFinish;
private ViewPager intro_images;
private LinearLayout pager_indicator;
private int dotsCount;
private ImageView[] dots;
private AdapterImageSlider mAdapter;
private int[] mImageResources = {
R.drawable.image_header_1,
R.drawable.image_header_2,
R.drawable.image_header_3
};
GridView gridview;
AdapaterGridView gridviewAdapter;
ArrayList<AdapterGridViewItem> data = new ArrayList<AdapterGridViewItem>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
intro_images = (ViewPager) v.findViewById(R.id.viewPage1);
pager_indicator = (LinearLayout) v.findViewById(R.id.viewPagerCountDots);
mAdapter = new AdapterImageSlider(getActivity(), mImageResources);
intro_images.setAdapter(mAdapter);
intro_images.setCurrentItem(0);
intro_images.setOnPageChangeListener(this);
setUiPageViewController();
gridview = (GridView) v.findViewById(R.id.gridView1);
data.add(new AdapterGridViewItem(getResources().getString(R.string.menu_product), ContextCompat.getDrawable(getActivity(), R.drawable.ic_product),0));
data.add(new AdapterGridViewItem(getResources().getString(R.string.menu_cart), ContextCompat.getDrawable(getActivity(), R.drawable.ic_cart),1));
data.add(new AdapterGridViewItem(getResources().getString(R.string.menu_checkout), ContextCompat.getDrawable(getActivity(), R.drawable.ic_checkout),2));
data.add(new AdapterGridViewItem(getResources().getString(R.string.menu_info), ContextCompat.getDrawable(getActivity(), R.drawable.ic_info),3));
data.add(new AdapterGridViewItem(getResources().getString(R.string.menu_profile), ContextCompat.getDrawable(getActivity(), R.drawable.ic_profile),4));
data.add(new AdapterGridViewItem(getResources().getString(R.string.menu_about), ContextCompat.getDrawable(getActivity(), R.drawable.ic_about),5));
setDataAdapter();
return v;
}
// Set the Data Adapter
private void setDataAdapter() {
gridviewAdapter = new AdapaterGridView(getActivity(), R.layout.lsv_item_mainmenu, data);
gridview.setAdapter(gridviewAdapter);
}
private void setUiPageViewController() {
dotsCount = mAdapter.getCount();
dots = new ImageView[dotsCount];
for (int i = 0; i < dotsCount; i++) {
dots[i] = new ImageView(getActivity());
dots[i].setImageDrawable(getResources().getDrawable(R.drawable.nonselecteditem_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(4, 0, 4, 0);
pager_indicator.addView(dots[i], params);
}
dots[0].setImageDrawable(getResources().getDrawable(R.drawable.selecteditem_dot));
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotsCount; i++) {
dots[i].setImageDrawable(getResources().getDrawable(R.drawable.nonselecteditem_dot));
}
dots[position].setImageDrawable(getResources().getDrawable(R.drawable.selecteditem_dot));
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
适配器代码是:
package com.erlanggastudio.myagri.adapters;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.erlanggastudio.myagri.R;
import com.erlanggastudio.myagri.activities.ActivityAbout;
import com.erlanggastudio.myagri.activities.ActivityCart;
import com.erlanggastudio.myagri.activities.ActivityCheckout;
import com.erlanggastudio.myagri.activities.ActivityInformation;
import com.erlanggastudio.myagri.activities.ActivityMenuCategory;
import com.erlanggastudio.myagri.activities.ActivityProfile;
import java.util.ArrayList;
public class AdapaterGridView extends ArrayAdapter<AdapterGridViewItem> {
Context mContext;
int resourceId;
ArrayList<AdapterGridViewItem> data = new ArrayList<AdapterGridViewItem>();
public AdapaterGridView(Context context, int layoutResourceId, ArrayList<AdapterGridViewItem> data) {
super(context, layoutResourceId, data);
this.mContext = context;
this.resourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = convertView;
ViewHolder holder = null;
if (itemView == null) {
final LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = layoutInflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.btnItem = (Button) itemView.findViewById(R.id.buttonMenu);
holder.txtItem = (TextView) itemView.findViewById(R.id.name);
itemView.setTag(holder);
} else {
holder = (ViewHolder) itemView.getTag();
}
final AdapterGridViewItem item = getItem(position);
holder.btnItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (item.getPoss() == 0) {
mContext.startActivity(new Intent(mContext, ActivityMenuCategory.class));
} else if (item.getPoss() == 1) {
mContext.startActivity(new Intent(mContext, ActivityCart.class));
} else if (item.getPoss() == 2) {
mContext.startActivity(new Intent(mContext, ActivityCheckout.class));
} else if (item.getPoss() == 3) {
mContext.startActivity(new Intent(mContext, ActivityInformation.class));
} else if (item.getPoss() == 4) {
mContext.startActivity(new Intent(mContext, ActivityProfile.class));
} else {
mContext.startActivity(new Intent(mContext, ActivityAbout.class));
}
}
});
holder.btnItem.setCompoundDrawables(null, item.getImage(), null,null);
holder.txtItem.setText(item.getTitle());
return itemView;
}
static class ViewHolder {
Button btnItem;
TextView txtItem;
}
}
布局代码是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="10dp">
<Button
android:id="@+id/buttonMenu"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/button_bg"
android:paddingTop="20dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center_horizontal"
android:textSize="16sp"
android:paddingTop="10dp"/>
</LinearLayout>
</RelativeLayout>
我尝试使用以下方式在按钮中显示dinamicly图像:
holder.btnItem.setCompoundDrawables(null, item.getImage(), null,null);
但是按钮中的图像没有显示,这是我的结果:
我的代码有任何问题,或者我很想念shomething ..... ???
答案 0 :(得分:2)
试试这个,
holder.btnItem..setCompoundDrawablesWithIntrinsicBounds(0,item.getImage(),0,0);