如何制作Android网格视图 – 垂直间距动态?

如何制作Android网格视图 – 垂直间距动态?,第1张

概述我正在使用网格视图,其中每个项目包含图像和一些文本.文本大小是动态的.我遇到的问题是当文本太长时,它会被弃用.如果我使用高垂直间距,问题就出现了.但由于每次文本的长度不同,我不能对所有项目使用高垂直间距.对于小文本我没有问题. 我的layout.xml看起来像这样 <LinearLayout android:id="@+id/list_container_layout" andro 我正在使用网格视图,其中每个项目包含图像和一些文本.文本大小是动态的.我遇到的问题是当文本太长时,它会被弃用.如果我使用高垂直间距,问题就出现了.但由于每次文本的长度不同,我不能对所有项目使用高垂直间距.对于小文本我没有问题.

我的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;}@OverrIDepublic 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;     }}

我的子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" />

仅举一步说,我正在从代码中动态设置GrID 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>

ImageAdapter类:

public class ImageAdapter extends BaseAdapter      {private Context mContext;// Keep all Images in arraypublic 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"};// Constructorpublic ImageAdapter(Context c){    mContext = c;}@OverrIDepublic int getCount() {    return mThumbIDs.length;}@OverrIDepublic Object getItem(int position) {    return mThumbIDs[position];}@OverrIDepublic long getItemID(int position) {    return 0;}   /* @OverrIDe  public VIEw getVIEw(int position,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,VIEwGroup parent) {     ImageVIEw icon;icon = new ImageVIEw(mContext);LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);VIEw row=inflater.inflate(R.layout.row,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;}}

类:

public class MenuActivity extends Activity implements              OnItemClickListener,OnClickListener {  GrIDVIEw grIDVIEw;    @OverrIDeprotected 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);}   @OverrIDepublic 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网格视图 – 垂直间距动态?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125096.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存