我在android中动态生成视图时遇到了问题。 我正在进行相对布局,我正在生成动态视图。但这只是重叠第一个和第二个视图。我搜索并尝试了更多。任何人都可以帮我解决这个问题。谢谢。这是我试过的代码。 Dynamicdataadapter.java 包com.Nithya.dynamiclist;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
public class Dynamicdataadapter extends BaseAdapter {
Context context;
ArrayList<String> data;
int layoutid;
RelativeLayout itemlayout;
ArrayList<Integer> textviewsize = new ArrayList<Integer>();
ArrayList<Integer> buttonsize = new ArrayList<Integer>();
public Dynamicdataadapter(Context context,ArrayList<String> datas, int childlayout) {
this.context = context;
this.data = datas;
this.layoutid = childlayout;
textviewsize.add(2);
textviewsize.add(5);
textviewsize.add(3);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView ;
Object[] textObjects = textviewsize.toArray();
if(row == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutid, parent, false);
itemlayout = (RelativeLayout)row.findViewById(R.id.itemlayout);
for(int room = 0 ; room < data.size() ; room++) {
if(position == room) {
TextView[] textview = new TextView[(Integer)textObjects[room]];
int previousid = 0;
int currentid = 0;
RelativeLayout rl = new RelativeLayout(context);
rl.setId(123456);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rl.setLayoutParams(lp);
for(int i =0 ; i < textview.length;i++) {
currentid = previousid + 1;
textview[i] = new TextView(context);
textview[i].setId(i);
textview[i].setText("text"+i);
RelativeLayout.LayoutParams lp_btn1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp_btn1.addRule(RelativeLayout.RIGHT_OF,(textview[i].getId()-1));
/*if(i > 0) {
lp_btn1.addRule(RelativeLayout.RIGHT_OF,previousid);
}*/
textview[i].setLayoutParams(lp_btn1);
//textview[i].setPadding(0, 0, 10, 0);
rl.addView(textview[i]);
previousid = currentid;
}
itemlayout.addView(rl);
}
}
}
return row;
}
}
MainActivity.java
package com.Nithya.dynamiclist;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
ListView dynamiclistview;
ArrayList<String> datas;
ArrayList<String> datalist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dynamiclistview = (ListView)findViewById(R.id.dynamiclistview);
datas = new ArrayList<String>();
datalist = new ArrayList<String>();
datas.add("abc");
datas.add("efg");
datas.add("hij");
Dynamicdataadapter dynamicadapter = new Dynamicdataadapter(this,datas,R.layout.childlayout);
dynamiclistview.setAdapter(dynamicadapter);
readdatafromassests();
}
public void readdatafromassests() {
AssetManager assetmanager = getAssets();
try {
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(assetmanager.open("datasheet")));
String readline ;
StringBuilder builder = new StringBuilder();
while((readline = bufferedreader.readLine()) != null) {
Log.d("datas", ""+readline);
if(readline.contains("Textview")) {
Log.d("datasheet datas", ""+readline);
}
builder.append(readline);
}
String datasheet = builder.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
使用LinearLayout而不是RelativeLayout
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView ;
Object[] textObjects = textviewsize.toArray();
if(row == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutid, parent, false);
itemlayout = (LinearLayout)row.findViewById(R.id.itemlayout);
for(int room = 0 ; room < data.size() ; room++) {
if(position == room) {
TextView[] textview = new TextView[(Integer)textObjects[room]];
int previousid = 0;
int currentid = 0;
LinearLayout rl = new LinearLayout(context);
rl.setId(123456);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
lp.setLayoutDirection(LinearLayout.HORIZONTAL);
rl.setLayoutParams(lp);
for(int i =0 ; i < textview.length;i++) {
currentid = previousid + 1;
textview[i] = new TextView(context);
textview[i].setId(i);
textview[i].setText("text"+i);
LinearLayout.LayoutParams lp_btn1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp_btn1.setLayoutDirection(LinearLayout.HORIZONTAL);
//lp_btn1.addRule(RelativeLayout.RIGHT_OF,(textview[i].getId()-1));
/*if(i > 0) {
lp_btn1.addRule(RelativeLayout.RIGHT_OF,previousid);
}*/
textview[i].setLayoutParams(lp_btn1);
//textview[i].setPadding(0, 0, 10, 0);
rl.addView(textview[i]);
previousid = currentid;
}
itemlayout.addView(rl);
}
}
}
return row;
}
答案 1 :(得分:0)
不确定是否是问题,但您在此处使用否定ID:
// first iteration i == 0
// setId(0)
textview[i].setId(i);
// RelativeLayout.RIGHT_OF, -1
lp_btn1.addRule(RelativeLayout.RIGHT_OF,(textview[i].getId()-1));
ID为-1的TextView不存在。