我正在使用网格 View ,其中每个项目都包含一个图像和一些文本。文本的大小是动态的。我遇到的问题是当文本太长时,它会被弃用。如果我使用高垂直间距,问题就会出现。但是由于每次文本的长度都不同,我不能为所有项目使用高垂直间距。对于小文本我没有问题。
我的 layout.xml 看起来像这样
<LinearLayout
android:id="@+id/list_container_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="@dimen/widget_padding"
android:background="@color/content_layout_bg"
android:orientation="vertical" >
<GridView
android:id="@+id/grid_workflow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:verticalSpacing="@dimen/widget_padding" >
</GridView>
</LinearLayout>
我的 Adapter 类如下所示:
public class WorkflowAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> nameValues;
private ViewHolder viewHolder;
private final int resourceId;
public WorkflowAdapter(Context context, int resourceId,ArrayList<String> nameValues) {
super(context,resourceId,nameValues);
this.context = context;
this.nameValues = nameValues;
this.resourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(resourceId, parent, false);
viewHolder = new ViewHolder();
viewHolder.label = (TextView) convertView.findViewById(R.id.txt_workflow_child);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
String menuItem = nameValues.get(position);
viewHolder.label.setText(menuItem);
return convertView;
}
public class ViewHolder {
TextView label;
}
}
我的 child layout.xml 看起来像这样
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/rounded_corner_ed"
android:orientation="vertical"
android:padding="@dimen/widget_padding" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/workflow_child_bg"
android:orientation="vertical"
android:padding="@dimen/widget_padding" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/widget_padding"
android:layout_marginRight="@dimen/widget_padding"
android:src="@drawable/icon64x64" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/txt_workflow_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/widget_padding"
android:text="TextView"
android:textColor="@color/text_color"
android:textSize="@dimen/text_size" />
顺便提一下,我正在从代码动态设置网格 View 的列宽。
任何帮助任何人?
最佳答案
嗨请使用这个例子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuActivity" >
<include
android:id="@+id/include1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/activity_header" />
<View
android:id="@+id/view"
android:layout_below="@+id/include1"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#CCCFD6" />
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/view"
android:gravity="center"
android:background="@drawable/gray_menu"
android:verticalSpacing="20dp"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
图像适配器类:
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e, R.drawable.f,
R.drawable.g, R.drawable.h, R.drawable.i,
R.drawable.j, R.drawable.k, R.drawable.l,
R.drawable.m, R.drawable.n, R.drawable.o
};
String[] titles = {"ddsdxc","xcxc","Allersdfgies",
"Immsdunidsdzation","sdfdsdc","Faczxmily",
"Mezdiczccations","Alerzxct","Insuraxcnce",
"Surgezxries","Teszxct","Hezxclth",
"Emezxrgzxczency","Medizxccal","Exzxizxczt"};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
/* @Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
return imageView;
}*/
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView icon;
icon = new ImageView(mContext);
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.row, parent, false);
TextView label=(TextView)row.findViewById(R.id.image_name);
label.setText(titles[position]);
icon=(ImageView)row.findViewById(R.id.album_image);
icon.setImageResource(mThumbIds[position]);
return row;
}
}
类(class):
public class MenuActivity extends Activity implements OnItemClickListener,OnClickListener
{
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_menu);
initUi();
}
private void initUi()
{
gridView = (GridView) findViewById(R.id.grid_view);
gridView.setNumColumns(3);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// TODO Auto-generated method stub
switch (arg2)
{
case 0:
intent=new Intent(this, Psfssdfs.class);
startActivity(intent);
break;
case 1:
intent=new Intent(this, CActivity.class);
startActivity(intent);
break;
case 2:
intent=new Intent(this, AActivity.class);
startActivity(intent);
break;
case 3:
intent=new Intent(this, IActivity.class);
startActivity(intent);
break;
case 4:
//intent=new Intent(MenuActivity.this, .PonActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
//startActivity(intent);
break;
case 5:
intent=new Intent(this, FnagerActivity.class);
startActivity(intent);
break;
case 6:
intent=new Intent(this, MeomeActivity.class);
startActivity(intent);
break;
case 7:
//intent=new Intent(MenuActivity.this, PconActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
//startActivity(intent);
break;
case 8:
intent=new Intent(this, Inzivity.class);
startActivity(intent);
break;
case 9:
intent=new Intent(this, PrzxcsActivity.class);
startActivity(intent);
break;
case 10:
intent=new Intent(this, TestsActivity.class);
startActivity(intent);
break;
case 11:
//intent=new Intent(MenuActivity.this, PersonazxvconActivity.class);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
//startActivity(intent);
break;
case 12:
intent=new Intent(this, Emergzxcctivity.class);
startActivity(intent);
break;
case 13:
intent=new Intent(this, MedicczxcActivity.class);
startActivity(intent);
break;
case 14:
showAlert();
break;
}
}
谢谢.. 我认为这很有帮助。
关于android - 如何制作 Android Grid View - 动态垂直间距?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19905302/