我想创建一个我没有任何XML文件的应用程序。 网格视图的图像和文本是动态添加的,我希望它看起来像文字在图像上方..我想要它。你可以说它看起来像图库上方有图像名称的图库。
所以请帮助我..
提前感谢你。
答案 0 :(得分:2)
即使没有(单个xml文件)main.xml,也不知道为什么需要这个。以下是您需要遵循的步骤。
通过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));
}
现在您可以根据需要定义CustomAdapter类。正如您所说,您需要图像上方的文本,因此您的每个网格项目现在应包含带图像的文本视图。可以使用 setOrientation(LinearLayout.VERTICAL)的线性布局实现此网格项,并将所有此类项目添加到gridview。
这个link可以为您提供如何创建自定义适配器类的基本知识。但在您的情况下,您动态定义网格项而不是xml。我刚刚给了你建议来达到你的要求,因为其他人很难完全编写你所需的代码。
答案 1 :(得分: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;
}
}