-1

我想制作一个没有任何 XML 文件的应用程序。网格视图的图像和文本是动态添加的,我希望它看起来像文本在图像上方。我想要它。你可以说它看起来像在图像上方有图像名称的画廊。

所以请帮帮我..

提前谢谢你。

4

2 回答 2

2

不知道为什么需要这个,即使没有(单个 xml 文件)main.xml。以下是您需要遵循的步骤。

  1. 通过 LayoutParams 动态创建布局,如下所示。

    公共类 MainActivity 扩展 Activity {

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            LayoutParams params=new GridView.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
            GridView layout=new GridView(this);
            layout.setNumColumns(9);
            layout.setAdapter(new CustomAdapter(this));
    
        }
    
  2. 现在根据您的要求定义 CustomAdapter 类。正如您所说,您需要图像上方的文本,因此您的每个网格项目现在应该包含一个带有图像的文本视图。这个网格项可以通过setOrientation(LinearLayout.VERTICAL)的线性布局来实现 ,并将所有此类类型的项添加到您的网格视图中。

链接可以让您基本了解如何创建自定义适配器类。但是在您的情况下,您过于动态地定义网格项而不是在 xml 中。我只是给了你实现你的要求的建议,因为其他人很难完整地编写你需要的代码。

于 2012-09-21T11:03:08.843 回答
0
Finally I made it on my own. And sharing to all if some one want to use it.

package com.example.customlayout;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity  {

    int i,j;
    int counter=0;
    boolean flag=false;
    int tid=55523,cid=25;
    int imid=55423,l_id=0;
    SharedPreferences settings;
    String [] values= new String[]{"Sample1","Sample2","Sample3","Sample4","Sample5","Sample6","Sample7","Sample8","Sample9","Sample10","Sample11","Sample12","Sample13","Sample14","Sample15","Sample16","Sample17","Sample18","Sample19","Sample20"};
    private Integer[] mThumbIds = {
            R.drawable.sample_1, R.drawable.sample_2,
            R.drawable.sample_3, R.drawable.sample_4,
            R.drawable.sample_5, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_1, R.drawable.sample_2,
            R.drawable.sample_3, R.drawable.sample_4,
            R.drawable.sample_5, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,

    };
    int pos=0;
    public static ArrayList<Integer> check = new ArrayList<Integer>(); 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ScrollView sc = new ScrollView(getApplicationContext());
        sc.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        LinearLayout ll = new LinearLayout(getApplicationContext());
        ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        ll.setOrientation(LinearLayout.VERTICAL);

        for( i = 0;i<10;i++)
        {
            final LinearLayout ll1 = new LinearLayout(getApplicationContext());
            ll1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            ll1.setOrientation(LinearLayout.HORIZONTAL);
            for(j = 0;j<2;j++)
            {

                ll1.addView(rowView(getApplicationContext()));
            }
            ll.addView(ll1);
        }
        sc.addView(ll);
        this.setContentView(sc);
    }

    private View rowView(Context context) {
        // TODO Auto-generated method stub

        WindowManager mWinMgr = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        int displayWidth = mWinMgr.getDefaultDisplay().getWidth();

        final LinearLayout ll1 = new LinearLayout(getApplicationContext());
        ll1.setLayoutParams(new LayoutParams((displayWidth/2), LayoutParams.WRAP_CONTENT));
        ll1.setOrientation(LinearLayout.VERTICAL);
        ll1.setId(l_id);

        final LinearLayout ll2 = new LinearLayout(getApplicationContext());
        ll2.setLayoutParams(new LayoutParams((displayWidth/2), LayoutParams.WRAP_CONTENT));
        ll2.setOrientation(LinearLayout.HORIZONTAL);
        ll2.setId(l_id);


        final TextView tv = new TextView(context);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        tv.setId(tid--);
        tv.setPadding(20, 0, 0, 0);
        tv.setText(values[pos]);

        final CheckBox cb = new CheckBox(getApplicationContext());

        cb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        cb.setId(cid--);
        cb.setTag(counter);

        for(int x = 0 ; x < check.size(); x++)
        {

            if(check.contains(counter))
            {
                cb.setChecked(true);
            }
        }
        counter++;

        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    Integer pos1= (Integer) buttonView.getTag();
                    check.add(pos1);
                    Log.i("TAG_3", ""+pos1);

                }else{
                    Integer pos2= (Integer) buttonView.getTag();
                    if(check.contains(pos2))
                    {
                    check.remove(pos2);
                    Log.i("TAG", ""+pos2+"removed");
                    }

                }
            }
        });


        final ImageView img = new ImageView(context);
        img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        img.setId(imid--);
        img.setScaleType(ImageView.ScaleType.CENTER_CROP);
        img.setPadding(8, 8, 8, 8);

        img.setImageResource(mThumbIds[pos]);

        pos++;
        /*Log.i("Demo", ""+pos);
        Log.i("TAG", ""+i);
        Log.i("TAG1", ""+j);
        Log.i("TAG2", ""+tid);
        Log.i("TAG3", ""+imid);*/

        ll2.addView(cb);
        ll2.addView(tv);
        ll1.addView(ll2);
        ll1.addView(img);
        ll1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(), "ID of TextView is:"+ String.valueOf(tv.getId())+" \t ID of Imageview is:"+ String.valueOf(img.getId()), Toast.LENGTH_SHORT).show();    
            }


        });
        l_id++; 

        return ll1;
    }



}
于 2012-10-03T09:56:18.583 回答