安卓问题与布局

时间:2011-08-05 10:52:57

标签: android layout gridview

我的代码有问题(它不按我想要的方式工作)。我有一组图像(图像的数量可能会有所不同)。所以我把它们放在网格视图中。下面是我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutRest" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:layout_alignParentLeft="true"
        android:layout_width="50px" android:id="@+id/adbutton"
        android:layout_height="fill_parent" android:text="\n\nR\nE\nK\nL\nA\nML\nA\nR\n\n"
        android:textStyle="bold">
    </Button>
    <ScrollView android:id="@+id/scrollViewRest"
        android:paddingLeft="100px" android:layout_width="wrap_content"
        android:layout_height="fill_parent">
        <LinearLayout android:id="@+id/linearLayout1"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:orientation="horizontal">
            <GridView android:id="@+id/gridViewRest" android:gravity="center"
                android:stretchMode="columnWidth" android:horizontalSpacing="50px"
                android:layout_width="wrap_content" android:verticalSpacing="50px"
                android:columnWidth="150px" android:layout_height="fill_parent"
                android:numColumns="auto_fit"></GridView>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

对于gridview来说     android:columnWidth =“150px”android:layout_height =“fill_parent” 它只是填满了屏幕的一半。但是当它的800像素它工作正常。 由于系统是动态的,我不希望每次安装时都使用XML。 我需要这方面的帮助,以下是我的java文件以防万一:

public class RestListActivity extends Activity{
    //ImageButton [] buttons;
    RelativeLayout rl;
    int restCount=0;
    Resources a;
    int resIndex=0;
    boolean resFound=false;
    int last;
    int resCount=0;
    // TODO Auto-generated constructor stub
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.restlist);
        rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest);

        Log.i("asd","asd");

        a=this.getResources();
        int i=0x7f020000;
        boolean done=true;

        Log.i("asd","asd");
        ArrayList <Integer> restLogoIDs=new ArrayList<Integer>();
        try
        {
            for (i=0x7f020000 ;done&&i<0x7f020040;i++)
            {
                Log.i("rest buttons","----"+i);
                String name=a.getResourceName(i);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    restLogoIDs.add(new Integer(i));
                    if(!resFound)
                    {
                        resIndex=i;
                        resFound=true;
                    }
                    Log.i("rest buttons",name);

                    resCount++;
                }
            }
        }
        catch(Exception e)
        {
            last=i;
            Log.i("LAST",i+"");
            done=false;
            e.printStackTrace();
        }

        Button orderButton = (Button)findViewById(R.id.adbutton);
        orderButton.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                Log.i("ACTION","AD BUTTON CLICKED");
                try
                {
                    Intent i = new Intent(RestListActivity.this,AVMSystemActivity.class);   
                    startActivity(i);
                }
                catch(Exception e)
                {
                    Log.i("cort",e.toString());
                }
            } 
        });

        //************************************************************************************
        //************************************************************************************
        //************************************************************************************

        GridView gridview = (GridView) findViewById(R.id.gridViewRest);

        ImageAdapter ia=new ImageAdapter(this);
        ia.setImageIDs(restLogoIDs);
        ia.setResources(a);
        gridview.setAdapter(ia);

    }
}




public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ArrayList <Integer> restLogoIDs=new ArrayList<Integer>();
    Resources res;

    public ImageAdapter(Context c) {
        mContext = c;
    }
    public void setResources(Resources r)
    {
        res=r;
    }
    public void setImageIDs(ArrayList <Integer> restLogoIDs)
    {
        this.restLogoIDs=restLogoIDs;
    }
    public int getCount() {
        return restLogoIDs.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("=============","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");    
        int i=0x7f020000;
        boolean done=true;

        try
        {
            for (i=0x7f020000 ;done&&i<0x7f020040;i++)
            {
                Log.i("rest buttons","----"+i);
                String name=res.getResourceName(i);
                name=name.substring(name.indexOf(':')+1);
                name=name.substring(9);
                if(name.startsWith("rest"))
                {
                    //restLogoIDs.add(new Integer(i));
                    Log.i("rest buttons",name);                         
                }
            }
        }
        catch(Exception e)
        {
            Log.i("LAST",i+"");
            done=false;
            e.printStackTrace();
        }
         ImageView imageView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(restLogoIDs.get(position));
            return imageView;
    }
    // references to our images
}

谢谢你们

1 个答案:

答案 0 :(得分:0)

我不知道问题是否存在,但我建议你分别使用dip和sp作为资源和字体。有关详细信息,请参阅here